This commit is contained in:
many2many 2024-09-26 09:46:54 +08:00
parent 1469c2683c
commit 2211015ff5

View File

@ -0,0 +1,30 @@
// 父类
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return "some animal sound";
}
}
// 子类
class Dog extends Animal {
speak() {
return this.name +": Woof!";
}
}
// 子类
class Cat extends Animal {
speak() {
return this.name +": Meow!";
}
}
// 使用
let dog = new Dog("阿狗");
let cat = new Cat("阿猫");
console.log(dog.speak()); // 输出: Woof!
console.log(cat.speak()); // 输出: Meow!