zoukankan      html  css  js  c++  java
  • JavaScript 类的继承

    类的继承

    1 子承父业

    extends(继承父类的普通函数)(方法)

    class Father {
                constructor() {
                }
                money() {
                    console.log(100);    
                }
            }
            class Son extends Father {
            }
            class sunzi extends Son {
    
            }
            var yxf = new Father;
            var lbw = new Son;
            var bb = new sunzi;
            console.log(yxf.money());
            console.log(lbw.money());
            console.log(bb.money());

    super的用法

    用于访问和调用对象父类上的函数。可以调用父类的构造函数,也可以调用父类的普通函数(方法)

    class Father1 {
                constructor(x,y) {
                    this.x = x;
                    this.y = y;
                }
                sum() {
                    console.log(this.x + this.y);
                }
            }
            class Son1 extends Father1 {
                constructor(x,y){
                    super(x,y);
                }
            }
            var yxf = new  Son1(1,2);
            yxf.sum();

    super关键字调用就近原则

        <script>
            //super关键字调用父类普通函数
            class Father {
                say() {
                    return '我是爸爸';
                }
            }
            class Son extends Father {
                say() {
                //     return '我是儿子';
                console.log( super.say());
                
                }
            }
            var yxf = new Son();
            yxf.say();//返回结果:我是儿子  就近原则
            //继承中的属性或方法查找原则:就近原则
            //1.继承中,如果实例化子类输出一个方法,先看子类有没有这个方法,如果有就先执行子类;
            //2.继承中,如果子类里面没有,就去查找父类有没有如果有就用父类
            
            
        </script>

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

    注意:子类子构造函数使用super 必须放到this的前面(必须先调用父类的构造方法 再使用子类的构造方法)父亲永远是第一位的!!!!

        <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;
                }
                sub() {
                    console.log(this.x - this.y);
                }
            }
            var son = new Son(1,2);
            son.sum();
            son.sub();
        </script>
        <script>
            //super关键字调用父类普通函数
            class Father {
                say() {
                    return '我是爸爸';
                }
            }
            class Son extends Father {
                say() {
                //     return '我是儿子';
                console.logsuper.say());
                
                }
            }
            var yxf = new Son();
            yxf.say();//返回结果:我是儿子  就近原则
            //继承中的属性或方法查找原则:就近原则
            //1.继承中,如果实例化子类输出一个方法,先看子类有没有这个方法,如果有就先执行子类;
            //2.继承中,如果子类里面没有,就去查找父类有没有如果有就用父类
            
            
        </script>
  • 相关阅读:
    实验axios用户登录及token验证
    vue的路由跳转了,可是页面没有变化
    搞清楚cookie,session,token,JWT
    APP应用测试流程
    Scrum敏捷开发
    APP项目环境及发布平台
    ssh -T git@github.com ssh: connect to host github.com port 22: Connection timed out
    pytest与unitest区别
    数据库三大范式--大白话
    docker 安装mysql挂载/var/lib/mysql目录时启动就退出
  • 原文地址:https://www.cnblogs.com/qiujie-prion/p/12989572.html
Copyright © 2011-2022 走看看