1.ts中不支持继承多个类, 但是类可以作为接口来实现

  1. // Person 类
  2. class Person {
  3. name: string;
  4. sayHello() {
  5. console.log("tag", `Helo ${this.name}!`);
  6. }
  7. }
  8. // Student 类
  9. class Student {
  10. grade: number;
  11. study() {
  12. console.log("tag", " I need Study!");
  13. }
  14. }
  15. class SmartObject implements Person, Student {
  16. // Person
  17. name: string = "person";
  18. sayHello: () => void;
  19. // Activatable
  20. grade: number = 3;
  21. study: () => void;
  22. }

2.接口可以实现多继承

  1. // 阿里接口
  2. interface Ali {
  3. pay: () => void;
  4. }
  5. // 腾讯接口
  6. interface Tencent {
  7. game: string;
  8. play: () => void;
  9. }
  10. // 自己的接口
  11. interface Self extends Ali, Tencent
  12. // 这样谁implement Self的话就必须同事实现Ali和Tencent和Self定义的所有属性和方法