zoukankan      html  css  js  c++  java
  • 面向对象编程--继承2

    四://原型式继承
      //原型是继承
      function inheritObject(o){
        //声明一个过度函数对象
        function F(){}
        //过渡对象的原型继承父对象
        F.prototype = o;
        //返回过度对象的一个实例,该实例的原型继承了父对象
        return new F();
      }
      // 原型式继承是对类式继承的一个封装,其中的过度对象就相当于类式继承中的子类,只不过在原型式中作为一个过度对象出现的,目的式为了创建要返回的新的实例化对象。


      var book = {
        name: 'js Book',
        alikeBook: ['css','html','python']
      };
      var newBook = inheritObject(book);
      newBook.name = "ajax book";
      newBook.alikeBook.push("javascript设计模式")

      var otherBook = inheritObject(book);
      otherBook.name = "css book";


      console.log(newBook.name) //["css", "html", "python", "javascript设计模式"]
      console.log(newBook.alikeBook) //ajax book

      console.log(otherBook.name) //css book
      console.log(otherBook.alikeBook) //["css", "html", "python", "javascript设计模式"]

      // 缺点:和类式继承一样,父对象book中的值类型的属性被复制引用类型的属性被共用

    //寄生式继承
      
      function inheritObject(o){
        //声明一个过度函数对象
        function F(){}
        //过渡对象的原型继承父对象
        F.prototype = o;
        //返回过度对象的一个实例,该实例的原型继承了父对象
        return new F();
      }
      //声明基对象
      var book = {
        name: 'js Book',
        alikeBook: ['css','html','python']
      };
      function createBook(obj){
        //通过原型继承方式创建新对象
        var o = new inheritObject(obj)
        //扩展对象
        o.getName = function(){
          console.log(name)
        };
        //返回新对象
        return o
       }
      var b1 = createBook(book)
      console.log(b1)
      b1.name = "jax book";
      b1.alikeBook.push("Linux")

      var b2 = createBook(book)
      console.log(b1)
      b2.name = "css book";

      console.log(b1.name)
      console.log(b1.alikeBook) //["css", "html", "python", "Linux"]
      console.log(b2.name)
      console.log(b2.alikeBook) //["css", "html", "python", "Linux"]

    // 寄生组合式继承
      // 寄生组合式继承是哪两种模式?
      // 寄生是寄生式继承,寄生式继承依托于原型继承,原型继承由于类式继承相似,另一种应该式构造函数继承。这里寄生式继承它处理的不是对象,而是类的原型。
      /**
        *寄生式继承 继承原型
        *传递参数 subClass 子类
        *传递参数 superClass 父类
      **/
      function inheritObject(o){
        //声明一个过度函数对象
        function F(){}
          //过渡对象的原型继承父对象
          F.prototype = o;
          //返回过度对象的一个实例,该实例的原型继承了父对象
          return new F();
        }
        function inheritPrototype(subClass, superClass){
          //复制一份父类的原型副本保存在变量中
          var p = inheritObject(superClass.prototype);
          //修正因为重写子类原型导致子类的constructoe属性被修改
          p.constructor = subClass
          //设置子类的原型
          subClass.prototype = p
        }


        //定义父类
        function SuperClass(name){
          this.name = name;
          this.color = ['red','green','blue'];
        }
        //定义父类原型的方法
        SuperClass.prototype.getName = function(){
          console.log(this.name)
        }
        //定义子类
        function SubClass(name,time){
          //构造函数式继承
          SuperClass.call(this,name)
          //子类添加新属性
          this.time = time;
        }
        //寄生式继承父类原型
        inheritPrototype(SubClass,SuperClass);
        //为子类新增原型方法
        SubClass.prototype.getTime = function(){
          console.log(this.time)
        }

        //创建
        var instance1 = new SubClass("js book", 2019);
        var instance2 = new SubClass("css book", 2018);

        instance1.color.push("blank");
        console.log(instance1.color) //["red", "green", "blue", "blank"]
        console.log(instance2.color) //["red", "green", "blue"]

    寄生组合式继承 最大的改变就是对子类原型的处理,被赋予父类原型的一个引用,这是一个对象。

  • 相关阅读:
    python 基础到字符串学习
    Newtonsoft.Json 获取匿名类数据
    Abp Wcf结合使用问题
    Ef Migration 操作出现SQLEXPRESS
    No context type was found in the assembly 'xxx.xxxx'. CodeFirst Ef错误
    Ef Code First 发生”provider: SQL Network Interfaces, error: 26
    ef 多条数据插入处理方案(据说还有更好的)
    记录一次 HttpWebRequest 尝试自动重定向太多 错误
    NetCore 下使用RSA加密,解密;并且前端使用jsencrypt.js实现Rsa相关方法。
    The specified framework version '2.0' could not be parsed 错误处理
  • 原文地址:https://www.cnblogs.com/sklhtml/p/9922133.html
Copyright © 2011-2022 走看看