js中的多态和继承
来源:互联网 发布:云雀知止的叫是母的吗 编辑:程序博客网 时间:2024/06/03 18:15
封装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);
1 0
- js中的多态和继承
- js中的原型和继承
- JS中的继承和Extend
- js中的类,对象和继承
- 如何理解JS中的封装和继承?
- Js中的原型继承和原型链
- 使用JS中的Call方法实现继承和多重继承
- python中的多继承和多态
- Java中的继承和多态
- C中的继承和多态
- C#中的接口和继承多态
- Java中的继承和多态
- C中的继承和多态
- C++中的多态和继承
- C#中的继承和多态
- Java中的继承和多态
- c++中的继承和多态
- C中的继承和多态
- Cocos2d-x中的CC_CALLBACK_X详解
- scrapy-redis 和 scrapy 有什么区别?
- java已知圆点坐标和半径,得到圆上的点
- 总结: C++ 中如何把输出结果写入到文件中
- Genymotion配置及使用教程(最新最完整版附各部分下载地址)
- js中的多态和继承
- processing视频教程收集
- Java总结篇系列:Java多线程(一)
- MySQL之——主从复制的一些参数配置
- scrapy爬去过程遇到的问题的解决办法
- 一种IC验证装置或/和IC测试装置STE_V2.3
- 双链表
- Codevs 1629 01迷宫
- python中的类方法和静态方法