zoukankan      html  css  js  c++  java
  • javascript中的封装多态和继承

    封装Encapsulation

    如下代码,这就算是封装了

    (function (windows, undefined) {
        var i = 0;//相对外部环境来说,这里的i就算是封装了
    })(window, undefined);

    继承Inheritance

    (function (windows, undefined) {
        //父类
        function Person() { }
        Person.prototype.name = "name in Person";
     
        //子类
        function Student() { }
        Student.prototype = new Person();           //修复原型
        Student.prototype.constructor = Student;    //构造函数
        Student.prototype.supr = Person.prototype;  //父类
     
        //创建子类实例
        var stu = new Student();
        Student.prototype.age = 28;
        Student.prototype.name = "name in Student instance";
     
        //打印子类成员及父类成员
        alert(stu.name); //name in Student instance
        alert(stu.supr.name); //name in Person
        alert(stu.age); //28
     
    })(window, undefined);

    多态Polymorphism

    有了继承,多态就好办了

    //这就是继承了
    (function (windows, undefined) {
        //父类
        function Person() { }
        Person.prototype.name = "name in Person";
        Person.prototype.learning = function () {
            alert("learning in Person")
        }
     
        //子类
        function Student() { }
        Student.prototype = new Person();           //修复原型
        Student.prototype.constructor = Student;    //构造函数
        Student.prototype.supr = Person.prototype;  //父类
        Student.prototype.learning = function () {
            alert("learning in Student");
        }
     
        //工人
        function Worker() { }
        Worker.prototype = new Person();           //修复原型
        Worker.prototype.constructor = Worker;    //构造函数
        Worker.prototype.supr = Person.prototype;  //父类
        Worker.prototype.learning = function () {
            alert("learning in Worker");
        }
     
        //工厂
        var personFactory = function (type) {
            switch (type) {
                case "Worker":
                    return new Worker();
                    break;
                case "Student":
                    return new Student();
                    break;
            }
            return new Person();
        }
     
        //客户端
        var person = personFactory("Student");
        person.learning(); //learning in Student
        person = personFactory("Worker");
        person.learning(); //learning in Worker
     
    })(window, undefined);


    ------------------------------------------
    除非特别声明,文章均为原创,版权与博客园共有,转载请保留出处
    BUY ME COFFEE
  • 相关阅读:
    Ubuntu下的OpenResty 安装
    你真的理解devDependencies和dependencies区别吗?
    "No user exists for uid 501"
    Mongo的备份和恢复(mongodump 和mongorestore )
    mongodb最大连接数、最大连接数修改
    ssh 断开解决办法
    Know How To Use Check Box Mapping Of Other Values Property In Oracle Forms
    Date Picker Calendar For Oracle Forms 6i
    Freebie: Date Picker Calendar Demo Form For Oracle Forms 6i
    Creating Excel File in Oracle Forms
  • 原文地址:https://www.cnblogs.com/kkun/p/2314885.html
Copyright © 2011-2022 走看看