首页 > 代码库 > [TypeScript] Sharing Class Behavior with Inheritance in TypeScript
[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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。