日期:2014-05-16 浏览次数:20460 次
function Animal() {
//私有变量
var _name;
var _color;
//公共变量
this.type = "我是动物";
//特权变量
this.setName = function (name) {
_name = name;
}
this.getName = function () {
return _name;
}
this.setColor = function (color) {
_color = color;
}
this.getColor = function () {
return _color;
}
//私有方法
function Action() {
alert("我在大便,不要看!");
}
//公用方法
this.Show = function () {
alert("我是" + _name + ",我的颜色是" + _color);
}
//静态方法
Animal.Extend = function (NewObj) {
NewObj.prototype = new Animal();
NewObj.prototype.constructor = NewObj["constructor"];
NewObj.prototype.method_d = NewObj["method_d"];
return NewObj;
}
}
var newObj = {
// 定义公共属性
prototy_b: '123',
//定义构造函数
constructor: function () {
// 定义私有属性
var _id = '123456';
// 定义特权属性
this.name = 'bbbb';
// 定义特权方法
this.getId = function () {
return _id;
};
this.setId = function (id) {
_id = id;
}
},
//定义方法
method_d: function () {
alert("这是dog类的继承方法");
}
};
var aimal = new Animal();
var dog= Animal.Extend(newObj );
<SCRIPT LANGUAGE="JavaScript">
2 <!--
3 function dwn(s)
4 {
5 document.write(s+'<br/>');
6 }
7
8 //定义一个Collection类型
9 function Collection(size)
10 {
11 this.size = function(){return size}; //公有方法,可以被继承
12 }
13
14 //定义一个_Collection类型
15 function _Collection(_size)
16 {
17 this._size = function(){return _size}; //公有方法,可以被继承
18 }
19
20 Collection.prototype.isEmpty = function() //静态方法,不能被继承
21 {
22 return this.size() == 0;
23 }
24
25 //定义一个ArrayList类型,它“继承”Colleciton类型
26 function ArrayList()
27 {
28 var m_elements = []; //私有成员,不能被继承
29 m_elements = Array.apply(m_elements,arguments);
30
31 //ArrayList类型继承Colleciton
32 this.base = Collection;
33 this.base.call(this,m_elements.length);
34
35 this.base = _Collection; //可以实现多态继承
36 this.base.call(this,m_elements.length);
37
38 this.add = function()
39 {
40 return m_elements.push.apply(m_elements,arguments);
41