javascript
// 声明一个简单的类
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound.`);
}
}
// 继承 Animal 类的子类
class Dog extends Animal {
constructor(name, breed) {
super(name); // 调用父类的构造函数
this.breed = breed;
}
speak() {
console.log(`${this.name} barks loudly.`);
}
}
// 创建类的实例并调用方法
const myDog = new Dog('Buddy', 'Golden Retriever');
myDog.speak(); // 输出 "Buddy barks loudly."