• 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>
  • 相关阅读:
    4G(LTE)背后的技术和利益纠结——VoIP,VoLTE,SIP,IMS的前世今生
    Windows抓取本地回环数据包
    SIP中的早期媒体与回铃音的产生
    SpringMVC整合
    浮点数转换为十进制
    将Sublime Text 2搭建成一个好用的IDE
    python3 'gbk' codec can't decode byte 0x80 in position 读取文件编码集错误的一个bug
    Matplotlib入门教程
    turtle教程-Python绘图
    python画图模块之一:turtle(1) 画五角星、正方形等
  • 原文地址:https://www.cnblogs.com/qiujie-prion/p/12989572.html
Copyright © 2020-2023  润新知