面试题答案
一键面试class Animal {
speak() {
console.log('动物发出声音');
}
}
class Dog extends Animal {
speak() {
console.log('汪汪汪');
}
}
class Cat extends Animal {
speak() {
console.log('喵喵喵');
}
}
// 测试多态性
const animal1 = new Dog();
const animal2 = new Cat();
animal1.speak();
animal2.speak();