1、请编码实现动物世界的继承关系:
动物(Animal)具有行为:吃(eat)、睡觉(sleep)
动物包括:兔子(Rabbit),老虎(Tiger)
这些动物吃的行为各不相同(兔子吃草,老虎吃肉);但睡觉的行为是一致的。
请通过继承实现以上需求,并进行测试。
class Animal{
constructor(sleep){
this.sleep = sleep
}
sleep(){
console.log(this.sleep)
}
}
class Rabbit extends Animal{
constructor(sleep,eat){
super(sleep)
this.eat = eat
}
eat1(){
console.log('兔子')
super.sleep()
console.log(this.eat)
}
}
class Tiger extends Animal{
constructor(sleep,eat){
super(sleep)
this.eat = eat
}