日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

typescript继承和重写

發布時間:2023/12/31 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 typescript继承和重写 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

類的繼承和重寫

在這里通過一個例子,說一下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-子類的一個方法!");}public sayHello(){super.sayHello();console.log("child-重寫父類的方法,添加新的東西!");}}let child = new Child('xiaoxiao','boy',2);child.play();child.sayHello();

說明:

  • 子類通過 extends 繼承父類

  • 子類可以重新定義父類中的方法進行重寫(比如上面例子中的sayHello)

  • 注意:

    TS只能單層繼承

    總結

    以上是生活随笔為你收集整理的typescript继承和重写的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。