zoukankan      html  css  js  c++  java
  • 通过一段代码理解es6继承;

    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方法;

  • 相关阅读:
    作业
    作业4
    作业1
    作业
    补交课堂作业
    补交最后一题
    第三次作业
    作业
    C语言 homework(4)
    C语言 homework (3)
  • 原文地址:https://www.cnblogs.com/qqfontofweb/p/7568750.html
Copyright © 2011-2022 走看看