﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-hrch</title><link>http://www.blogjava.net/hrch/</link><description /><language>zh-cn</language><lastBuildDate>Thu, 07 May 2026 05:49:50 GMT</lastBuildDate><pubDate>Thu, 07 May 2026 05:49:50 GMT</pubDate><ttl>60</ttl><item><title>Ext基础-类的定义与使用</title><link>http://www.blogjava.net/hrch/archive/2009/09/06/294070.html</link><dc:creator>春华-秋实</dc:creator><author>春华-秋实</author><pubDate>Sun, 06 Sep 2009 08:08:00 GMT</pubDate><guid>http://www.blogjava.net/hrch/archive/2009/09/06/294070.html</guid><wfw:comment>http://www.blogjava.net/hrch/comments/294070.html</wfw:comment><comments>http://www.blogjava.net/hrch/archive/2009/09/06/294070.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hrch/comments/commentRss/294070.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hrch/services/trackbacks/294070.html</trackback:ping><description><![CDATA[<p><strong>创建一个类分两步走：<br />
第一步：定义构造函数，处理传入的初始化参数(完成属性的继承)<br />
第二步：实现继承，即增加新的方法。(完成方法的继承)</strong></p>
<p><strong>示例：</strong><br />
假设我们已有一个Person类，定义如下：<br />
var Person=function(config){<br />
&nbsp;&nbsp;Ext.apply(this,config);<br />
&nbsp;basename="hrch";<br />
&nbsp;baseMethod=function(){alert("a method of base class!")};<br />
&nbsp;};</p>
<p>我们想定义一个Student类，继承自Person：<br />
/*<br />
&nbsp;*说明：config中也可以包含方法，但最好不要这样做，新的方法放在第二步中定义。<br />
&nbsp;* /<br />
第一步：定义构造函数，完成初始化<br />
var Student=function(config){<br />
&nbsp;//Student.superclass.constructor这个对象由第二步获得，没有第二步，则这个地方会出错。<br />
&nbsp;Student.superclass.constructor.apply(this,config);//将config中的属性方法追加到Student对象中。<br />
}</p>
<p>/*<br />
&nbsp;*说明：这里也可以定义新的属性，但最好不要这样做，新的属性放在第一步中定义。<br />
&nbsp;* /<br />
第二步：实现继承<br />
Ext.extend(Student,Person,{<br />
&nbsp;newMethod1:function(){alert("newMethod1")},<br />
&nbsp;newMethod2:function(){alert("newMethod2")},<br />
});</p>
<p><strong>测试代码：</strong><br />
&lt;script type="text/javascript"&gt;<br />
&nbsp;&nbsp;&nbsp; var Person=function(config){<br />
&nbsp;&nbsp;&nbsp; &nbsp;Ext.apply(this,config);<br />
&nbsp;this.basename="hrch";<br />
&nbsp;this.baseMethod=function(){alert("a method of base class!")};<br />
&nbsp;};&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; var Student=function(config){<br />
&nbsp;//Student.superclass.constructor这个对象由第二步获得，没有第二步，则这个地方会出错。<br />
&nbsp;Student.superclass.constructor.call(this,config);//将config中的属性方法追加到Student对象中。<br />
&nbsp;&nbsp;&nbsp; };<br />
&nbsp;&nbsp;&nbsp; Ext.extend(Student,Person,{<br />
&nbsp;newMethod1:function(){alert("newMethod1");},<br />
&nbsp;newMethod2:function(){alert("newMethod2");}<br />
&nbsp;&nbsp;&nbsp; });</p>
<p>&nbsp;&nbsp;&nbsp; var stu=new Student({name:"student",age:"24"});<br />
&nbsp;&nbsp;&nbsp; alert(stu.name);<br />
&nbsp;&nbsp;&nbsp; alert(stu.age);<br />
&nbsp;&nbsp;&nbsp; stu.newMethod1();<br />
&nbsp;&nbsp;&nbsp; stu.newMethod2();<br />
&lt;/script&gt;<br />
&nbsp;</p>
<img src ="http://www.blogjava.net/hrch/aggbug/294070.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hrch/" target="_blank">春华-秋实</a> 2009-09-06 16:08 <a href="http://www.blogjava.net/hrch/archive/2009/09/06/294070.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>