码迷,mamicode.com
首页 > 其他好文 > 详细

[TypeScript] Sharing Class Behavior with Inheritance in TypeScript

时间:2016-10-14 07:36:24      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5958923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!