zoukankan      html  css  js  c++  java
  • Javascript的简单继承 简单

     function First(age, name) {
    this.age = age;
    this.name = name;
    }
    First.prototype.getAge = function() {
    return this.age;
    };
    First.prototype.toString = function() {
    return "[ age: " + this.age + ", name: " + this.name + " ]";
    };

    function Second(age, name, password) {
    First.call(this, age, name);
    this.password = password;
    }
    Second.prototype = new First();
    Second.prototype.constructor = Second;

    Second.prototype.getPassword = function() {
    return this.password;
    };
    Second.prototype.toString = function() {
    return "[ age: " + this.age + ", name: " + this.name + ", password: " + this.password + " ]";
    };
    //由于通过Second.prototype = new First();
    //继承了父类First的name和age属性,需要通过子类显示删除两个属性
    delete Second.prototype.age;
    delete Second.prototype.name;


    //继承First
    function Second2(age, name, sex) {
    First.call(this, age, name);
    this.sex = sex;
    }
    Second2.prototype.toString = function() {
    return "[ age: " + this.age + ", name: " + this.name + ", sex: " + this.sex + " ]";
    };
    Second2.prototype.getSex = function() {
    return this.sex;
    };

    function Third(age, name, password, address, email, sex) {
    Second.call(this, age, name, password);
    Second2.call(this, age, name, sex);
    this.address = address;
    this.email = email;
    }
    Third.prototype = new Second();
    Third.prototype.constructor = Third;
    Third.prototype.getAddress = function() {
    return this.address;
    };
    Third.prototype.setEmail = function(email) {
    this.email = email;
    };
    Third.prototype.toString = function() {
    return "[ age: " + this.age + ", name: " + this.name + ", password: " + this.password + ", address: " + this.address + ", email: " + this.email + " ]";
    };
    delete Third.prototype.name;
    delete Third.prototype.age;
    delete Third.prototype.password;

  • 相关阅读:
    git初学
    Android中activity的四个启动模式
    onsaveInstanceState有关问题
    default activity not found的问题
    实现随手指移动
    入园第一天
    玩转Django2.0---Django笔记建站基础八(admin后台系统)
    玩转Django2.0---Django笔记建站基础七(表单与模型)
    玩转Django2.0---Django笔记建站基础六(模型与数据库)
    玩转Django2.0---Django笔记建站基础五(模板)
  • 原文地址:https://www.cnblogs.com/chyong168/p/2256051.html
Copyright © 2011-2022 走看看