class animal{
    constructor(props){
        this.name = 'xiaoniao' || props.name
    }
    eat(){
        console.log(this.name+ 'is eating')
    }
}

class birds extends animal{
    constructor(props){
        super(props);
    }
    fly(){
        console.log(this.name+' is flying')
    }
}
var ying = new birds(); //实例化一个ying对象
ying.fly(); //调用birds的fly方法
ying.eat(); //调用eat方法继承自animal 这个类

这里class关键字是es6中特有的,与java语言类似, 也可以用extends 关键字实现继承; 实例化一个niao对象;这个对象继承了animal里面eat方法;自己扩展了fly方法;

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-31
  • 2021-11-18
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2021-08-03
猜你喜欢
  • 2021-11-07
  • 2021-07-22
  • 2021-04-13
  • 2021-05-30
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案