[TypeScript] Sharing Class Behavior with Inheritance in TypeScript

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[TypeScript] Sharing Class Behavior with Inheritance in TypeScript相关的知识,希望对你有一定的参考价值。

Typescript classes make inheritance much easier to write and understand. In this lesson we look into how to set up inheritance with Typescript classes, extends and super.

 

class ComicBookCharacter (
  constructor{
    public alias: string, public health: number , public strength: number,
    protected secretIdentity: string
  ) {}
}

class SuperVillain extends ComicBookCharacter {
  flaws = ["hubris", "always explains evil plan"];

  constructor(a, b, c, d) {
    console.log(${this.alias} eats kittens!!!);
    super(a, b, c, d);
  }  
}

 

To review, we can set up inheritance with the extends keyword. The protected access modifier can‘t be accessed outside of a class just like the private access modifier, but it can be accessed in derived classes.

If you don‘t define a constructor, the derived class will use the base class‘s constructor. If you do define the constructor in a derived class, super must be called before anything else can happen.

以上是关于[TypeScript] Sharing Class Behavior with Inheritance in TypeScript的主要内容,如果未能解决你的问题,请参考以下文章

TypeScript核心篇——类(class)-可选参数-存取器-构造函数-静态属性方法-抽象类

PAT甲级1032 Sharing (25分)

初始化参数之cursor_sharing

sharing-sphere单库分表(入门级示例)

sharing-sphere单库分表(入门级示例)

sharing-sphere单库分表(入门级示例)