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>
  • 相关阅读:
    Git ignore file for Xcode projects
    How can I create a zip archive of a whole directory via terminal without hidden files?
    What is a bare git repository?
    How to tell if UIViewController's view is visible
    Adding A Shadow To UIView
    Properties
    iOS中nil,Nil,NULL之间的区别
    FMDB的简单使用
    iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
    对retain 和 assign的理解
  • 原文地址:https://www.cnblogs.com/qiujie-prion/p/12989572.html
Copyright © 2011-2022 走看看