美文网首页
typescript 学习第五天

typescript 学习第五天

作者: 798b6b7c244d | 来源:发表于2020-08-05 11:14 被阅读0次

    接口扩展 接口可继承接口

    第一种

    interface Animal {
      eat():void;
    }
    
    interface Person extends Animal{
      work():void;
    }
    
    class Web implements Person{
      public name:string;
      constructor(name:string){
        this.name = name
      }
      eat(){
        console.log(this.name + '吃粮食')
      }
      work(){
        console.log(this.name + '敲代码')
      }
    }
    var w = new Web('老王');
    w.eat()
    

    第二种

    interface Animal{
      eat():void;
    }
    
    interface Penson extends Animal{
      work():void;
    }
    
    class Programmer {
      public name:string;
      constructor(name:string){
        this.name = name
      }
      coding(code:string){
        console.log(this.name + code)
      }
    }
    
    class Web extends Programmer implements Person{
      constructor(name:string){
        super(name)
      }
      eat(){
        console.log(this.name + '吃粮食')
      }
      work(){
        console.log(this.name + '敲代码')
      }
    }
    var w = new Web('小王')
    w.coding('敲代码')
    

    相关文章

      网友评论

          本文标题:typescript 学习第五天

          本文链接:https://www.haomeiwen.com/subject/eywsrktx.html