es6 class 继承

xiaoxiao2021-02-28  60

class Animal{

constructor(name,color){

this.name=name;

this.color=color

}

eat(){

return this.name+'吃饭 '

}

}

// 原型方法继承

Animal.prototype.run=(x)=>{

return x+'跑的快'

}

let dog=new Animal("hh",'red')

console.log(dog)

console.log(dog.eat())

console.log(dog.run('hh'))

class Cat extends Animal{

constructor(name,color,sex){

// 属性继承

super(name,color)

this.sex=sex

}

eat(){

// 方法继承

return super.eat()

}

}

 

let cat = new Cat('xx','black','woman')

console.log(cat)

console.log(cat.eat())

console.log(cat.run('xx'))

 

 

转载请注明原文地址: https://www.6miu.com/read-1950006.html

最新回复(0)