继承,封装,多态

// es5
        function person(name,age){
            this.name=name,
            this.age=age,
            this.study=function(){
                console.log(this,"好吃")
            }
        }
        // 使用构造函数在原型实例上添加方法
        person.prototype.drink=function(){
            console.log(this,"好吃")
        }
     
        const father=new person("王汉迪","18")
        // es5的继承
        function son(name,age,hobby){
            person.call(this,name,age),
            this.hobby=hobby
        }
        const s=new son("王汉迪","18","好吃")
        console.log(father,person.prototype.drink(),s);

// es6
        class animal{
            constructor(name,age){
                this.name=name,
                this.age=age
            }
            eat(){
                console.log(this.name,"xuexi")
                return "111"
            }
        }
        class dog extends animal{
            constructor(name,age){
                super(name,age)
            }
            eat(){
                console.log(this.name,"xue")
                // super.eat()
                return "111"
            }
        }
        var f=new animal("ji",1)
        var s1=new dog("goudan",2)
        console.log(f.eat(),s1.eat())

 

posted @ 2021-12-18 10:35  肥瘦皆宜  阅读(23)  评论(0编辑  收藏  举报