4.super.ts 732 B

123456789101112131415161718192021222324252627282930
  1. (function() {
  2. // 父类
  3. class Animal {
  4. name:string;
  5. color:string;
  6. constructor(name:string,color:string) {
  7. this.name = name;
  8. this.color = color;
  9. }
  10. eat() {
  11. console.log("好吃爱吃多吃")
  12. }
  13. }
  14. // 猫
  15. class Cat extends Animal {
  16. age:number;
  17. /**
  18. * 若子类继承父类
  19. * 子类的构造函数中必须对父类的构造函数进行重写
  20. */
  21. constructor(a:string,b:number,c:string) {
  22. super(a,c);
  23. this.age = b;
  24. }
  25. }
  26. let animal = new Animal("猫",'白');
  27. let cat = new Cat('妹妹',3,'黄')
  28. console.log(animal)
  29. console.log(cat)
  30. })()