javascript中的封装多态和继承
2012-01-06 16:48
786 查看
封装Encapsulation
如下代码,这就算是封装了
[code]vari=0;//相对外部环境来说,这里的i就算是封装了
[/code]
继承Inheritance
[code]//父类
[/code]
多态Polymorphism
有了继承,多态就好办了
[code](function(windows,undefined){
[/code]
如下代码,这就算是封装了
(function(windows,undefined){
[code]vari=0;//相对外部环境来说,这里的i就算是封装了
})(window,undefined);
[/code]
继承Inheritance
(function(windows,undefined){
[code]//父类
functionPerson(){}
Person.prototype.name="nameinPerson";
//子类
functionStudent(){}
Student.prototype=newPerson();//修复原型
Student.prototype.constructor=Student;//构造函数
Student.prototype.supr=Person.prototype;//父类
//创建子类实例
varstu=newStudent();
Student.prototype.age=28;
Student.prototype.name="nameinStudentinstance";
//打印子类成员及父类成员
alert(stu.name);//nameinStudentinstance
alert(stu.supr.name);//nameinPerson
alert(stu.age);//28
})(window,undefined);
[/code]
多态Polymorphism
有了继承,多态就好办了
//这就是继承了
[code](function(windows,undefined){
//父类
functionPerson(){}
Person.prototype.name="nameinPerson";
Person.prototype.learning=function(){
alert("learninginPerson")
}
//子类
functionStudent(){}
Student.prototype=newPerson();//修复原型
Student.prototype.constructor=Student;//构造函数
Student.prototype.supr=Person.prototype;//父类
Student.prototype.learning=function(){
alert("learninginStudent");
}
//工人
functionWorker(){}
Worker.prototype=newPerson();//修复原型
Worker.prototype.constructor=Worker;//构造函数
Worker.prototype.supr=Person.prototype;//父类
Worker.prototype.learning=function(){
alert("learninginWorker");
}
//工厂
varpersonFactory=function(type){
switch(type){
case"Worker":
returnnewWorker();
break;
case"Student":
returnnewStudent();
break;
}
returnnewPerson();
}
//客户端
varperson=personFactory("Student");
person.learning();//learninginStudent
person=personFactory("Worker");
person.learning();//learninginWorker
})(window,undefined);
[/code]
相关文章推荐
- JavaScript 定义类的最佳写法——完整支持面向对象(封装、继承、多态),兼容所有浏览器,支持用JSDuck生成文档
- JavaScript使用prototype原型实现的封装继承多态示例
- javascript中的封装,多态,继承
- JavaScript 面向对象(封装、继承、多态)多种方式实现完全总结
- 【JavaScript】JavaScript中的封装继承多态
- javascript中的封装多态和继承
- JavaScript----函数的封装、继承和多态
- JavaScript 利用prototype原型实现封装继承多态
- JavaScript的模块化:继承(原型)、封装(闭包)、多态
- 韩顺平 javascript教学视频_学习笔记19_js面向对象三大特征(封装,继承,多态)
- 用Javascript实现面向对象编程(封装,抽象,继承,多态)
- javascript OOP:实现继承、多态与封装
- JavaScript实现多态和继承的封装操作示例
- JavaScript基础--面向对象三大特性(八):继承封装多态
- 封装、继承、多态。
- 面向对象 -- Java中的封装,继承,多态(下)
- 实例讲解 封装继承多态(二)
- Java学习笔记——类与对象(封装、继承与多态)
- 类 对象 封装 继承 多态概念 [http://blog.zol.com.cn/818/article_817762.html]
- java基础三大特性——封装、继承与多态