TypeScript--接口的继承,接口和类搭配使用

接口的继承

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 + "写代码");
  }
}

接口和类搭配使用

interface Animal {
    
    
  eat(): void;
}

interface Person 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("写ts代码");

猜你喜欢

转载自blog.csdn.net/I_r_o_n_M_a_n/article/details/114700301