Javascript中使用面向对象的编程


http://www.cnblogs.com/seewood/archive/2005/06/24/180740.html
/**
* 一个js版的继承 多态 封装
* 继承链 XStudent-->Student-->Person-->Animal->Base
* 其中添加除了继承过的属性和方法
* 还添加了一些自己的方法
*/


/**
* 类,用来创建对象
* @type
*/
var Class = {
create : function() {
return function() {
//创建对象的初始化抽象方法
this.initialize.apply(this, arguments);
};
}
};
/**
* 用来复制对象
* @param {} desc 目标对象
* @param {} src 原对象
* @return {} 目标对象
*/
var Extend = function(desc, src) {
for (var property in src) {
desc[property] = src[property];
}
return desc;
};
/**
* 给Object原型添加方法Extend,用来实现继承
* @param {} obj 需要扩展的数组
* @return {} 继承后的目标对象
*/
Object.prototype.extend = function(obj) {
return Extend.apply(this, [this, obj]);//当前对象调用Extend,实现继承功能
};
var Base = Class.create();//创建对象Base
/**
* Base的原型中指定constructor
* 实现初始化initialize方法
* @type
*/
Base.prototype = {
constructor : Base,
initialize : function(v_Name) {
this.name = v_Name;
},
setName : function(v_Name) {
this.name = v_Name;
},
getName : function() {
return this.name;
}
};
var Animal = Class.create();//创建Animal
/**
* 指定Animal原型继承Base,
* 并重写了初始化方法initialize,
* 添加age,sex,weigth属性
* 添加了sleep方法,并给了默认的实现
* 添加抽象方法eat,walk,say
*/
Animal.prototype = (new Base()).extend({
constructor : Animal,
initialize : function(v_Name, v_Age, v_Sex, v_Weight) {
this.name = v_Name;
this.weight = v_Weight;
this.age = v_Age;
this.sex = v_Sex;
},
setAge : function(v_Age) {
this.age = v_Age;
},
getAge : function() {
return this.age;
},
setSex : function(v_Sex) {
this.sex = v_Sex;
},
getSex : function() {
return this.sex;
},
setWeight : function() {
this.weight = v_Weigth;
},
getWeight : function() {
return this.weight;
},
sleep : function() {
return this.name + "正在睡觉中..";
},
abstract : function() {
this.eat();
this.walk();
this.say();
}
});
var Person = Class.create();//创建人类
/**
* 指定人原型对象继承Animal
* 重写initialize
* 实现抽象方法eat,walk,say
* 添加属性address,desc
*/
Person.prototype = (new Animal()).extend({
constructor : Person,
initialize : function(v_Name, v_Age, v_Sex, v_Weight, v_Address,
v_Desc) {
this.name = v_Name;
this.weight = v_Weight;
this.age = v_Age;
this.sex = v_Sex;
this.address = v_Address;
this.desc = v_Desc;
},
setAge : function(v_Age) {
this.age = v_Age;
},
setAddress : function(v_Address) {
this.address = v_Address;
},
getAddress : function() {
return this.address;
},
setDesc : function(v_Desc) {
this.desc = v_Desc;
},
getDesc : function() {
return this.desc;
},
eat : function() {
return this.name + "正在吃肉和一些素食";
},
walk : function() {
return "人类用双脚走路";
},
say : function() {
return "hello!我是" + this.name + ",今年" + this.age + "岁";
}
});
var Student = Class.create();//创建Student
/**
* 指定学生原型对象继承人类
* 重写initialize, sleep, walk, say
* 添加公有属性Id
*/
Student.prototype = (new Person()).extend({
constructor : Student,
initialize : function(v_Id, v_Name, v_Age, v_Sex, v_Weight,
v_Address, v_Desc) {
this.id = v_Id;
this.name = v_Name;
this.weight = v_Weight;
this.age = v_Age;
this.sex = v_Sex;
this.address = v_Address;
this.desc = v_Desc;
},
getId : function() {
return this.id;
},
setId : function(v_Id) {
this.id = v_Id;
},
setAge : function(v_Age) {
this.age = v_Age;
},
sleep : function() {
return this.name + "躺在床上呼呼睡大觉";
},
walk : function() {
return this.name + "正在骑着自行车上学去了!!";
},
say : function() {
return "hello!我是" + this.name + ",是一个学生。";
}
});
var XStudent = Class.create();
XStudent.prototype = (new Student()).extend({
constructor : XStudent
});

var stu = new XStudent(1, 'fkshl', 25, '男', '55kg', 'yyyyy', '....!!!');
alert(stu.sleep());
alert(stu.constructor);
alert(XStudent.prototype.constructor);

var b = new Base('xx');
alert(b.getName());

var r = new Person('xyz', 25, '男', '55kg', 'yyyyy', '....!!!');
alert(r.sleep());
alert(r.walk());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值