概述

接口

+ 代码示例
接口
interface Animal {
  name:string;
  age:number;
  makeSound():void;
}

class Dog implements Animal {
  name:string;
  age:number;
  constructor(name:string, age:number) {
    this.name = name;
    this.age = age;
  }
  makeSound():void {
    console.log(this.name + "汪汪汪");
  }
}

let dog = new Dog("乐乐", 9);
dog.makeSound();

抽象类

+ 代码示例
抽象类
abstract class Shape {
  color:string;
  constructor(color:string) {
    this.color = color;
  }
  abstract getArea():number;
}

class Rectangle extends Shape {
  width:number;
  height:number;
  constructor(color:string, width:number, height:number) {
    super(color);
    this.width = width;
    this.height = height;
  }
  getArea():number {
    return this.width * this.width;
  }
}

const rectangle = new Rectangle("红色" 10 10)
console.log(rectangle.getArea());