1. class Animal {
    2. constructor(name) {
    3. this.name = name;
    4. }
    5. getName() {
    6. return this.name;
    7. }
    8. }
    9. class Dog extends Animal {
    10. constructor(name, age) {
    11. super(name);
    12. this.type = "animal";
    13. this.age = age;
    14. }
    15. }
    16. class Cat {
    17. constructor(age) {
    18. this.age = age;
    19. }
    20. }
    21. let dog = new Dog();
    22. dog.name = "中华田园犬";
    23. dog.age = 5;
    24. let cat = new Cat();
    25. cat.age = 4;
    26. cat.name = "布偶猫";
    27. console.log(333, dog,cat);//{name:'中华田园犬',age:5,type:'animal'} {name:'布偶猫',age:4}
    28. console.log(333,dog.getName());//中华田园犬
    29. console.log(333,cat.getName());//报错 cat.getName is not a function