类的继承

2021-04-23  本文已影响0人  ssttIsme
<!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>
上一篇下一篇

猜你喜欢

热点阅读