请编码实现动物世界的继承关系:
动物(Animal)具有行为:吃(eat)、睡觉(sleep)
动物包括:兔子(Rabbit),老虎(Tiger)
这些动物吃的行为各不相同(兔子吃草,老虎吃肉);但睡觉的行为是一致的。
请通过继承实现以上需求,并进行测试。
//方式一
// 1.定义Animal类
class Animal {
constructor(animal, eat, sleep) {
this.animal = animal
this.eat = eat
this.sleep = sleep
}
show() {
console.log(`${this.animal}的共同特性是: ${this.eat}, ${this.sleep}`)
}
}
//2.定义Rabbit类,继承自Animal
class Rabbit extends Animal {
constructor(animal, eat, sleep, grass, meat) {
super(animal, eat, sleep)
this.grass = grass
this.meat = meat
}
show() {
super.show()