自定义对象, 类对象和类原型的继承(Object.mix):可以模拟实现对象和类的链式继承和多重继承,但在类继承下,实例对象无法通过instanceof运算符来判断对象的父类
显示代码
function fInheritObject() {
// 对象定义或者静态类定义
var Person = {};
// var Person = Object.createClass("netease.mail.Person");
// 扩展静态类属性
Object.extend(Person, {
name : "人类",
say : function (sFrom) {
alert("我是" + this.name + (sFrom || ""));
}
});
// 对象定义或者静态类定义
var Father = {};
// var Father = Object.createClass("netease.mail.Father");
// Father继承Person,并覆盖属性和方法
Object.mix(Father, Person, {
name : "爸爸",
say : function () {
Father.superClass.say.call(this, ","+ this.name +"说:这是来自爸爸的调用");
}
});
// 对象定义或者静态类定义
var Mother = {};
// var Mother = Object.createClass("netease.mail.Mother");
// Mother继承Person,并覆盖属性和方法
Object.mix(Mother, Person, {
name : "妈妈",
say : function () {
Mother.superClass.say.call(this, ","+ this.name +"说:这是来自妈妈的调用");
}
});
// 对象定义或者静态类定义
var Son = {};
// var Son = Object.createClass("netease.mail.Son");
Object.mix(Son, Father); // Son继承Father
Object.mix(Son, Mother); // Son继承Mother
// 并覆盖属性和方法
Object.extend(Son, {
name : "儿子",
say : function () {
Son.superClass.say.call(this);
}
});
// 调用静态类方法
Person.say();
Father.say();
Mother.say();
Son.say();
}
显示代码
function fInheritClass() {
// 定义Person类
var Person = Object.createClass("netease.mail.Person");
// 扩展Person类属性
Object.extend(Person.prototype, {
name : "人类",
say : function (sFrom) {
alert("我是" + this.name + sFrom);
}
});
// 定义Father类
var Father = Object.createClass("netease.mail.Father");
// Father继承Person,并覆盖属性和方法
Object.mix(Father.prototype, new Person(), {
name : "爸爸",
say : function () {
Father.prototype.superClass.say.call(this, ","+ this.name +"说:这是来自爸爸的调用");
}
});
// 定义Mother 类
var Mother = Object.createClass("netease.mail.Mother");
// Mother继承Person,并覆盖属性和方法
Object.mix(Mother.prototype, new Person(), {
name : "妈妈",
say : function () {
Father.prototype.superClass.say.call(this, ","+ this.name +"说:这是来自妈妈的调用");
}
});
// 定义Son类
var Son = Object.createClass("netease.mail.Son");
Object.mix(Son.prototype, new Father());// Son继承Father
Object.mix(Son.prototype, new Mother());// Son继承Mother
// 并覆盖属性和方法
Object.extend(Son.prototype, {
name : "儿子",
say : function () {
Son.prototype.superClass.say.call(this);
}
});
var son = new Son(); // 新建Son的对象
son.say();// 调用对象方法
// 测试对象的instanceof
alert("son instanceof Son : " + (son instanceof Son) + "\nson instanceof Mother : " + (son instanceof Mother) + "\nson instanceof Father : " + (son instanceof Father) + "\nson instanceof Person : " + (son instanceof Person))
}
显示代码
function fInheritObjectAndClass() {
// 定义Person类
var Person = Object.createClass("netease.mail.Person");
// 扩展Person类属性
Object.extend(Person, {
name : "人类",
say : function (sFrom) {
alert("我们是" + this.name + (sFrom || ""));
}
});
Object.extend(Person.prototype, {
name : "无名氏",
initialize : function (sName) {
this.name = sName || this.name;
},
say : function (sFrom) {
alert("我是" + this.name + (sFrom || ""));
}
});
// 定义Father类
var Father = Object.createClass("netease.mail.Father");
// Father继承Person,并覆盖属性和方法
Object.mix(Father, Person, {
name : "爸爸"
});
Object.mix(Father.prototype, new Person());
Object.extend(Father.prototype, {
say : function () {
Father.prototype.superClass.say.call(this, ","+ this.name +"说:这是来自"+ Father.name +"的调用");
}
});
// 定义Mother 类
var Mother = Object.createClass("netease.mail.Mother");
// Mother继承Person,并覆盖属性和方法
Object.mix(Mother, Person, {
name : "妈妈"
});
Object.mix(Mother.prototype, new Person());
Object.extend(Mother.prototype, {
say : function () {
Mother.prototype.superClass.say.call(this, ","+ this.name +"说:这是来自"+ Mother.name +"的调用");
}
});
// 定义Son类
var Son = Object.createClass("netease.mail.Son");
// 继承类对象
Object.mix(Son, Father);
Object.mix(Son, Mother);
// 继承类的prototype
Object.mix(Son.prototype, new Father());
Object.mix(Son.prototype, new Mother());
// 并覆盖属性和方法
Object.extend(Son, {
name : "儿子"
});
Object.extend(Son.prototype, {
say : function () {
Son.prototype.superClass.say.call(this);
}
});
// 静态方法
Person.say();
Mother.say();
Father.say();
Son.say();
// 示例方法调用
var son = new Son("Harry"); // 新建Son的对象
son.say();// 调用对象方法
// 测试对象的instanceof
alert("son instanceof Son : " + (son instanceof Son) + "\nson instanceof Mother : " + (son instanceof Mother) + "\nson instanceof Father : " + (son instanceof Father) + "\nson instanceof Person : " + (son instanceof Person))
}
原生类继承(Object.inherit):原生的方式继承类,可以实现类的原型链式继承,无法实现对象继承和多重继承,但在类继承下,实例对象通过instanceof运算符可以判断对象的父类
显示代码
function fNativeInheritClass() {
// 定义Person类
var Person = Object.createClass("netease.mail.Person");
// 扩展Person类属性
Object.extend(Person.prototype, {
name : "人类",
say : function (sFrom) {
alert("我是" + this.name + (sFrom || ""));
}
});
// 定义Father类
var Father = Object.createClass("netease.mail.Father");
// Father继承Person
Object.inherit(Father, Person);
// 并覆盖属性和方法
Object.extend(Father.prototype, {
name : "爸爸",
say : function () {
Father.superClass.say.call(this, ","+ this.name +"说:这是来自爸爸的调用");
}
});
// 定义Mother 类
var Mother = Object.createClass("netease.mail.Mother");
// Mother继承Person
Object.inherit(Mother, Person);
// 并覆盖属性和方法
Object.extend(Mother.prototype, {
name : "妈妈",
say : function () {
Mother.superClass.say.call(this, ","+ this.name +"说:这是来自妈妈的调用");
}
});
// 定义Son类
var Son = Object.createClass("netease.mail.Son");
Object.inherit(Son, Father);// Son继承Father
Object.inherit(Son, Mother);// Son继承Mother, superClass被Mother的覆盖
// 并覆盖属性和方法
Object.extend(Son.prototype, {
name : "儿子",
say : function () {
Son.superClass.say.call(this);
}
});
var son = new Son();// 新建Son的对象
// 留意调用父类的方法
son.say();// 调用对象方法
// 测试对象的instanceof
alert("son instanceof Son : " + (son instanceof Son) + "\nson instanceof Mother : " + (son instanceof Mother) + "\nson instanceof Father : " + (son instanceof Father) + "\nson instanceof Person : " + (son instanceof Person))
}
类的混合继承(Object.mix):同时对一个类的类对象的非原生继承以及类的prototype对象原生继承
显示代码
function fNativeInheritClassAndInheritObject() {
// 定义Person类
var Person = Object.createClass("netease.mail.Person");
// 扩展Person类属性
Object.extend(Person, {
name : "人类",
say : function (sFrom) {
alert("我们是" + this.name + (sFrom || ""));
}
});
Object.extend(Person.prototype, {
name : "无名氏",
initialize : function (sName) {
this.name = sName || this.name;
},
say : function (sFrom) {
alert("我是" + this.name + (sFrom || ""));
}
});
// 定义Father类
var Father = Object.createClass("netease.mail.Father");
// Father继承Person,并覆盖属性和方法
Object.mix(Father, Person, {
name : "爸爸"
}, {
say : function () {
Father.superClass.say.call(this, ","+ this.name +"说:这是来自"+ Father.name +"的调用");
}
});
// 定义Son类
var Son = Object.createClass("netease.mail.Son");
Object.mix(Son, Father, {
name : "儿子"
}, {
say : function () {
Son.superClass.say.call(this);
}
});// Son继承Mother
Person.say();
Father.say();
Son.say();
var son = new Son("Harry"); // 新建Son的对象
son.say();// 调用对象方法
// 测试对象的instanceof
alert("son instanceof Son : " + (son instanceof Son) + "\nson instanceof Father : " + (son instanceof Father) + "\nson instanceof Person : " + (son instanceof Person))
}