3.继承.js 834 B

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