3.继承.ts 884 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // 立即执行函数
  2. // function fn1() {}
  3. // fn1()
  4. // (function () {})()
  5. (function() {
  6. // 父级
  7. class All {
  8. name:string;
  9. age: number;
  10. constructor(name:string,age:number) {
  11. this.name = name;
  12. this.age = age;
  13. }
  14. say() {
  15. console.log("你好");
  16. }
  17. }
  18. /**
  19. * 继承:
  20. * 因为想让多个子类同时拥有父类的属性及方法 所以采用继承
  21. * 继承后 子类就会拥有父类相同的内容
  22. * 子类的方法若于父类的方法相同 则会覆盖
  23. * 若想添加新的方法 直接在子类中添加即可
  24. */
  25. class A extends All {
  26. say() {
  27. console.log("哈哈")
  28. }
  29. back() {
  30. console.log("撤回")
  31. }
  32. }
  33. let aa = new A('图图',3);
  34. console.log(aa);
  35. aa.say()
  36. aa.back()
  37. })();