美文网首页
类的继承

类的继承

作者: ssttIsme | 来源:发表于2021-04-23 21:47 被阅读0次
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>类的继承</title>
        </head>
        <body>
            <script>
                class Animal{
                    constructor(name,age){
                        this.name=name;
                        this.age=age;
                    }
                    sayName(){
                        return this.name;
                    }
                    sayAge(){
                        return this.age;
                    }
                }
                
                class Dog extends Animal{
                    constructor(name,age,color){
                        super(name,age);
                        this.color=color;
                    }
                    sayColor(){
                        return `${this.name},今年:${this.age},颜色:${this.color}`;
                    }
                    sayAge(){
                        return "我今年"+super.sayAge()+"汪汪";
                    }
                }
                let dog=new Dog('旺财','2岁','黄色');
                document.write(dog.sayColor());
                document.write("<br/>");
                document.write(dog.sayAge());
            </script>
        </body>
    </html>
    

    相关文章

      网友评论

          本文标题:类的继承

          本文链接:https://www.haomeiwen.com/subject/oqphrltx.html