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>

运行结果:

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM