Typescript中接口的扩展,接口的继承

港控/mmm° 2022-10-25 14:23 381阅读 0赞
  1. // Person接口
  2. interface Person {
  3. name: string;
  4. eat(foot: string): void;
  5. }
  6. // Man接口继承Person接口
  7. interface Man extends Person{
  8. work(type: string): void;
  9. }
  10. class Web{
  11. public name: string;
  12. constructor(name: string) {
  13. this.name = name;
  14. }
  15. cording(code: string) {
  16. console.log(`${this.name} 在${code}`);
  17. }
  18. }
  19. // BigMan 继承 Web 并实现 Person 接口
  20. class BigMan extends Web implements Man {
  21. eat(foot: string) {
  22. console.log(this.name + '在吃' + foot);
  23. }
  24. work(type: string) {
  25. console.log(this.name + '工作类型'+ type);
  26. }
  27. }
  28. var bm = new BigMan("朱远航"); //
  29. bm.eat("大虾"); // 朱远航在吃大虾
  30. bm.work("打渔"); // 朱远航工作类型打渔
  31. bm.cording("写代码"); // 朱远航 在写代码

发表评论

表情:
评论列表 (有 0 条评论,381人围观)

还没有评论,来说两句吧...

相关阅读

    相关 TypeScript接口

    TypeScript 的核心原则之一是对值所具有的结构进行类型检查。我们使用接口(Interfaces)来定义对象的类型。`接口是对象的状态(属性)和行为(方法)的抽象(描述)

    相关 TypeScript 接口

    接口在使用中用来规范的作用,定义一个方法需要什么参数,什么返回类型,或者属性什么类型等,实现了接口的类就要遵循接口的规矩 interface ren{ shengao