es6 子类继承父类的方法同时扩展自己的方法

 

es6 子类继承父类的方法同时扩展自己的方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>子类继承父类的方法同时扩展自己的方法</title>
</head>
<body>

    <script>
        class Father {
            constructor(x,y){
                this.x =x;
                this.y =y;
            }
            sum(){
                console.log(this.x+this.y)
            }
        }

        // 子类继承父类的加法方法 同时扩展自己的减法方法
        class Son extends Father {
            constructor(x,y){
                //调用父类的构造方法
                //super必须在子类this之前调用
                super(x,y)
                this.x = x;
                this.y = y;

            }
            subtract() {
                console.log(this.x - this.y)
            }
        }
        var son = new Son(9,2)
        son.sum()
        son.subtract()

    </script>
</body>
</html>

运行结果:

------------恢复内容结束------------

posted @ 2020-05-05 21:13  前端那点事  阅读(1399)  评论(0)    收藏  举报