zoukankan      html  css  js  c++  java
  • 关于常见继承的几种方法

    一:关于继承
    1、原型链继承:
    原理:是将父类的实例,转换子类的原型
    优点:继承关系非常清晰
    易于实现
    缺点:来自原型对象引用的属性都是所有实例共享的
    创建子类的实例时,无法向父类构造函数传参
    2、构造函数继承:
    原理:将父类的构造函数来增强子类的实例,说白了就是将父类的实例属
    性复制给子类
    优点:解决了子类实例中共享父类引用的问题
    解决了创建子类实例时可以向父类传参,可以实现多继承(call多
    个父类对象)
    缺点:实例并不是父类的实例,而是子类的实例
    最大的弊端:只能继承父类的实例属性和方法,不能继承原型属性和方法
    3、实例继承:
    原理:为父类的实例添加新特性,作为子类的实例返回
    优点:不限制调用方式,主要使用实例继承,不管是new子类 还是直接调
    用子类()返回的对象都具有同样的效果
    缺点:实例时父类的实例不是子类的实例,不支持多继承
    4、拷贝继承
    原理:拷贝
    优点:支持多继承
    缺点:效率较低,占用内存高(因为要拷贝父类的属性给子类)
    5、组合继承
    原理:通过调用父类的构造函数 继承父类的属性 并保留传参的优点 通
    过父类的实例来做子类的原型,实现函数原型上的继承
    优点:弥补了构造函数继承的缺陷 可以继承父类原型上的属性
    既可以是子类的实例 也可以是父类的实例
    不存在引用共享的问题
    可传参
    缺点:调用了两次父类构造函数 生成了两份实例

    6、寄生组合继承
    原理:通过寄生的方式 在组合继承的基础上 避免初始化两次
    优点:堪称完美
    缺点:实现较为复杂


    1、原型继承例题:
      

    function Animal(name){
            this.name = name||"yangyang",
            this.dosomething = function(){
            console.log(this.name+"正在吃shit"
            }
               }
        Animal.prototype.eat = function(food){
            console.log(this.name+"正在学习"+food
        
        }

    2、构造函数继承

    function Person(){
            
        }
        Person.prototype = new Animal()
        Person.prototype.name = "haha"
        var taotao = new Person();
        console.log(taotao.name)
        taotao.eat("apple")
        taotao.dosomething()
    
        function Person(){
            Animal.call(this,"wangjie")    
        }
        var taotao = new Person()
        console.log(taotao.name)
        taotao.dosomething()
    
        console.log(taotao.eat("yu"))
        console.log(taotao instanceof Animal)//false
        console.log(taotao instanceof Person)//true


    3、实例继承

    function Person(){
    var instance = new Animal
    instance.name = "rubin"
    return instance
    }
    
    var taotao = Person()
    console.log(taotao.name)
    taotao.dosomething()
    taotao.eat("yu")
    console.log(taotao instanceof Animal)
    console.log(taotao instanceof Person)


    4、拷贝继承

    function Person(){
    var instance = new Animal()
    for(var p in instance){
    Person.prototype[p] = instance[p]
    }
    Person.prototype.name = "wangjing"
    }
    var taotao = new Person()
    console.log(taotao.name)
    taotao.dosomething()
    console.log(taotao instanceof Animal)
    console.log(taotao instanceof Person)

    5、组合继承

    function Person(name){
    Animal.call(this,name)
    this.name = name || "hongxia" 
    }
    Person.prototype = new Animal()
    var taotao = new Person()
    console.log(taotao.name)
    taotao.dosomething()
    console.log(taotao instanceof Animal)
    console.log(taotao instanceof Person)

    6、寄生组合继承

    function Person(){
    Animal.call(this)
    this.name = "sunfei"
    }
    (function(){
    var Super = function(){}
    Super.prototype = Animal.prototype;
    Person.prototype = new Super()
    })();
    var taotao = new Person()
    console.log(taotao.name)
    taotao.dosomething()
    console.log(taotao instanceof Animal)
    console.log(taotao instanceof Person)



  • 相关阅读:
    JDBC学习笔记一
    MySql学习笔记四
    MySql学习笔记三
    MySql学习笔记二
    将select 转为json
    这个网站病毒挺有意思,下载我网站的图片,我说怎么爬虫爬我几十个G的图片
    学习新知识的“填--捋--磨”策略
    谈下程序设计算法的准备心得与体会-nCov隔离也许帮你提升能力
    关于青少年,编程,教育的一些感悟(写在农历鼠年前)
    数据库敏捷版本控制之3个数据库策略
  • 原文地址:https://www.cnblogs.com/xuxiaoxia/p/7460658.html
Copyright © 2011-2022 走看看