4.super.ts 781 B

12345678910111213141516171819202122232425262728293031323334
  1. // (function(){})()
  2. (function() {
  3. class Money {
  4. name: string;
  5. num: number;
  6. constructor(name:string,num:number) {
  7. this.name = name;
  8. this.num = num;
  9. }
  10. say() {
  11. console.log("你好")
  12. }
  13. }
  14. /**
  15. * super
  16. * 如果子类要使用父类中属性
  17. * 子类的构造函数必须对父类的构造函数重新进行接受
  18. * 使用super
  19. */
  20. class A extends Money {
  21. age:number;
  22. constructor(name:string,num:number,age:number) {
  23. super(name,num);
  24. this.age=age;
  25. }
  26. }
  27. class B extends Money {
  28. }
  29. let a = new A('喜羊羊',100,18);
  30. let b = new B('灰太狼',50);
  31. console.log(a,'a');
  32. console.log(b,'b')
  33. })()