<div class="codetitle"> <span><a style="CURSOR: pointer" data="89132" class="copybut" id="copybut89132" onclick="doCopy('code89132')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code89132"> <br> <br> <br><script type="text/javascript"><!-- <BR>ClassModel = //类模型,用于创建类 <BR>{ <BR>create: function() <BR>{ <BR>return function(){this.construct.apply(this,arguments);} <BR>} <BR>} <BR>Extend = function(desc, src) //模拟类继承, 将一个对象的所有成员 复制到 另一个对象中 <BR>{ <BR>for(var c in src) <BR>{ <BR>desc[c] = src[c]; <BR>} <BR>return desc; <BR>} <BR>Object.prototype.extend = function(src) <BR>{ <BR>return Extend.apply(this, [this, src]); <BR>} <BR>var human = ClassModel.create(); <BR>human.prototype = <BR>{ <BR>construct : function() //构造函数 <BR>{ <BR>//alert("construct method"); <BR>//alert(this.speak() + "," + this.sleep()); <BR>}, <BR>speak : function() <BR>{ <BR>alert("speak"); <BR>}, <BR>sleep : function() <BR>{ <BR>alert("sleep"); <BR>}, <BR>sex : function() <BR>{ <BR>alert("女"); <BR>} <BR>} <BR>var h = new human(); <BR>h.speak(); //调用human类的speak方法 <BR>var student = ClassModel.create(); <BR>student.prototype = (new human()).extend({ //student类继承类human类 <BR>sex : function() //方法重载 (多态) <BR>{ <BR>alert("男"); <BR>}, <BR>study : function() <BR>{ <BR>alert("studying"); <BR>}, <BR>thinking : function() <BR>{ <BR>alert("thinking"); <BR>} <br><br><BR>}); <BR>var student = new student(); <BR>student.sleep(); //调用 父类(human) 的sleep方法 <BR>student.study(); //调用 student的study方法 <BR>student.thinking(); //调用 student的thinking方法 <BR>student.sex(); //结果为 男 不再是父类的 女 <br><br>// --></script> <br> <br> <br> </div>