typescript继承和重写
生活随笔
收集整理的這篇文章主要介紹了
typescript继承和重写
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
類的繼承和重寫
在這里通過一個(gè)例子,說一下typescript繼承和重寫
//父類class Gege{public name:string;public age:number;public sex:string;public constructor(sex:string,name:string,age:number){this.name = name;this.age = age;this.sex = sex;}public say(){console.log("father-123456");}public sayHello(){console.log("father-123456");}}let ge:Gege = new Gege('youchen','boy',16); //和 constructor 保持一致//子類class Child extends Gege{public look:string = 'handsome';public play(){console.log("child-子類的一個(gè)方法!");}public sayHello(){super.sayHello();console.log("child-重寫父類的方法,添加新的東西!");}}let child = new Child('xiaoxiao','boy',2);child.play();child.sayHello();說明:
子類通過 extends 繼承父類
子類可以重新定義父類中的方法進(jìn)行重寫(比如上面例子中的sayHello)
注意:
TS只能單層繼承
總結(jié)
以上是生活随笔為你收集整理的typescript继承和重写的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 冲筋是什么意思
- 下一篇: mongDB批量插值