zoukankan      html  css  js  c++  java
  • super必须放到子类this之前

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</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 调用父类的构造函数
                    // super 必须在子类this之前调用
                    super(x, y);
                    this.x = x;
                    this.y = y;
    
                }
                subtract() {
                    console.log(this.x - this.y);
    
                }
            }
            var son = new Son(5, 3);
            son.subtract();
            son.sum();
        </script>
    </body>
    
    </html>

  • 相关阅读:
    双飞翼布局 & 圣杯布局
    php正则
    面向对象-object对象
    面向对象-赋值运算
    面向对象-作用域
    js高级-面向对象
    8.5学习笔记
    10.22
    10.19
    react路由
  • 原文地址:https://www.cnblogs.com/juham/p/14703387.html
Copyright © 2011-2022 走看看