TypeScript 中的多类继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34513594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:11:05  来源:igfitidea点击:

Multiple Class Inheritance In TypeScript

javascriptclassinheritanceinterfacetypescript

提问by nourikhalass

What are ways to get around the problem of only being allowed to extend at most one other class.

有什么方法可以解决最多只能扩展一个其他类的问题。

class Bar {

  doBarThings() {
    //...
  }

}

class Bazz {

  doBazzThings() {
    //...
  }

}

class Foo extends Bar, Bazz {

  doBarThings() {
    super.doBarThings();
    //...
  }

}

This is currently not possible, TypeScript will give an error. One can overcome this problem in other languages by using interfaces but solving the problem with those is not possible in TypeScript.

目前这是不可能的,TypeScript 会报错。可以通过使用接口在其他语言中克服这个问题,但在 TypeScript 中无法解决这些问题。

Suggestions are welcome!

欢迎提出建议!

采纳答案by Brocco

This is possible with interfaces:

这可以通过接口实现:

interface IBar {
  doBarThings();
}

interface IBazz {
  doBazzThings();
}

class Foo implements IBar, IBazz {
  doBarThings() {}
  doBazzThings(){}
}

But if you want implementation for this in a super/baseway, then you'll have to do something different, like this:

但是如果你想以super/base方式实现它,那么你必须做一些不同的事情,像这样:

class FooBase implements IBar, IBazz{
  doBarThings() {}
  doBazzThings(){}
}

class Foo extends FooBase {
  doFooThings(){
      super.doBarThings();
      super.doBazzThings();
  }
}

回答by nomadoda

This is my workaround on extending multiple classes. It allows for some pretty sweet type-safety. I have yet to find any major downsides to this approach, works just as I would want multiple inheritance to do.

这是我扩展多个类的解决方法。它允许一些非常甜蜜的类型安全。我还没有发现这种方法的任何主要缺点,就像我希望多重继承那样工作。

First declare interfaces that you want to implement on your target class:

首先声明要在目标类上实现的接口:

interface IBar {
  doBarThings(): void;
}

interface IBazz {
  doBazzThings(): void;
}

class Foo implements IBar, IBazz {}

Now we have to add the implementation to the Fooclass. We can use class mixins that also implements these interfaces:

现在我们必须将实现添加到Foo类中。我们可以使用也实现这些接口的类 mixins:

class Base {}

type Constructor<I = Base> = new (...args: any[]) => I;

function Bar<T extends Constructor>(constructor: T = Base as any) {
  return class extends constructor implements IBar {
    public doBarThings() {
      console.log("Do bar!");
    }
  };
}

function Bazz<T extends Constructor>(constructor: T = Base as any) {
  return class extends constructor implements IBazz {
    public doBazzThings() {
      console.log("Do bazz!");
    }
  };
}

Extend the Fooclass with the class mixins:

Foo使用类 mixins扩展类:

class Foo extends Bar(Bazz()) implements IBar, IBazz {
  public doBarThings() {
    super.doBarThings();
    console.log("Override mixin");
  }
}

const foo = new Foo();
foo.doBazzThings(); // Do bazz!
foo.doBarThings(); // Do bar! // Override mixin

回答by spierala

Not really a solution to your problem, but it is worth to consider to use composition over inheritance anyway.

不是您问题的真正解决方案,但无论如何都值得考虑使用组合而不是继承。

Prefer composition over inheritance?

更喜欢组合而不是继承?