[TOC] ##继承 ``` class demo{ constructor(public name:string){ } } class foo extends demo{ getname():string{ return this.name } } ``` ## 调用父类的方法 ``` class demo{ constructor(public name:string){ } getname(){ console.log(`parent ${this.name}`) } } class foo extends demo{ constructor(name:string){ //调用父类的构造函数 super(" change "+name) } getname(){ //调用父类的同名方法 super.getname() } } ``` ## readonly修饰符 ``` class demo{ readonly age:number=23 constructor(public readonly name:string){ } setname(name:string){ this.name=name; // 不能重新赋值 } setage(age:number){ this.age=age //不能重新赋值 } } ``` ## 存取器 `getter/setters` 注意属性名`_name` 和get/set的方法名不可相同`name()` ``` class demo{ private _name:string get name():string{ return `by get ${this._name}` } set name(name:string){ this._name=name } } var d = new demo() d.name="asd" d.name;//by get asd ``` ## 静态 ``` class demo{ static name:string="123" static getname(){ return "this is static as"; } } demo.name; demo.getname(); ``` ## 抽象类 声明`abstract `关键字的需在子类中具体实现 ``` abstract class demo{ abstract name:string; //需在子类中具体实现 age:number; abstract getname(name:string):string; setname():string{; return "this is a setname"; } } class d extends demo{ name: string; getname(name:string): string { return this.name=name } } ``` ## 把类当做接口使用 ``` class Point { x: number; y: number; } interface Point3d extends Point { z: number; } let point3d: Point3d = {x: 1, y: 2, z: 3}; ```