You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
536 B

2 years ago
  1. // 可见修饰符:protected 表示受保护的,仅对声明所在类和子类中(非实例对象)可见
  2. // 父类
  3. class Animal {
  4. // move 方法是受保护的
  5. protected move() {
  6. console.log('123')
  7. }
  8. run() {
  9. console.log('789')
  10. }
  11. }
  12. const a = new Animal()
  13. // 实例对象不能访问 move 方法
  14. // a.move()
  15. // 子类
  16. class Dog extends Animal {
  17. bark() {
  18. console.log('456')
  19. }
  20. }
  21. const d = new Dog()
  22. // 子类的实例对象也不能访问 move 方法
  23. // d.move()