zoukankan      html  css  js  c++  java
  • 面向对象的程序设计(九)寄生组合式继承

    function object(o) {
        function F() {}
        F.prototype = o;
        return new F();
    }
    
    function inheritPrototype(subType, superType) {
        var prototype = object(superType.prototype);//创建对象
        prototype.constructor = subType;//增强对象
        subType.prototype = prototype;//指定对象
    }
    
    function SuperType(name) {
        this.name = name;
        this.colors = ["red", "blue"];
    }
    
    SuperType.prototype.sayName = function () {
        alert(this.name);
    }
    
    function SubType(name, age) {
        SuperType.call(this, name);
    
        this.age = age;
    }
    
    inheritPrototype(SubType, SuperType);
    
    SubType.prototype.sayAge = function () {
        alert(this.age);
    }
    
    var instance1 = new SubType("Tom", "22");
    instance1.colors.push("black");
    console.dir(instance1);
    
    var instance2 = new SubType("Lucy", "25");
    console.dir(instance2);
  • 相关阅读:
    异常
    带参数的方法
    变量,基本类型,数据类型和运算符
    数据类型转换(针对数字类型)
    this关键字
    面向对象DAO模式
    常见类 Object
    方法和包
    final关键字
    abstract关键字
  • 原文地址:https://www.cnblogs.com/qiangspecial/p/3176461.html
Copyright © 2011-2022 走看看