hrch

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            1 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks

          2009年9月6日 #

          創建一個類分兩步走:
          第一步:定義構造函數,處理傳入的初始化參數(完成屬性的繼承)
          第二步:實現繼承,即增加新的方法。(完成方法的繼承)

          示例:
          假設我們已有一個Person類,定義如下:
          var Person=function(config){
            Ext.apply(this,config);
           basename="hrch";
           baseMethod=function(){alert("a method of base class!")};
           };

          我們想定義一個Student類,繼承自Person:
          /*
           *說明:config中也可以包含方法,但最好不要這樣做,新的方法放在第二步中定義。
           * /
          第一步:定義構造函數,完成初始化
          var Student=function(config){
           //Student.superclass.constructor這個對象由第二步獲得,沒有第二步,則這個地方會出錯。
           Student.superclass.constructor.apply(this,config);//將config中的屬性方法追加到Student對象中。
          }

          /*
           *說明:這里也可以定義新的屬性,但最好不要這樣做,新的屬性放在第一步中定義。
           * /
          第二步:實現繼承
          Ext.extend(Student,Person,{
           newMethod1:function(){alert("newMethod1")},
           newMethod2:function(){alert("newMethod2")},
          });

          測試代碼:
          <script type="text/javascript">
              var Person=function(config){
               Ext.apply(this,config);
           this.basename="hrch";
           this.baseMethod=function(){alert("a method of base class!")};
           };  
              var Student=function(config){
           //Student.superclass.constructor這個對象由第二步獲得,沒有第二步,則這個地方會出錯。
           Student.superclass.constructor.call(this,config);//將config中的屬性方法追加到Student對象中。
              };
              Ext.extend(Student,Person,{
           newMethod1:function(){alert("newMethod1");},
           newMethod2:function(){alert("newMethod2");}
              });

              var stu=new Student({name:"student",age:"24"});
              alert(stu.name);
              alert(stu.age);
              stu.newMethod1();
              stu.newMethod2();
          </script>
           

          posted @ 2009-09-06 16:08 春華-秋實 閱讀(213) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 河北区| 无锡市| 犍为县| 凭祥市| 涞源县| 张家川| 曲靖市| 阿荣旗| 当阳市| 贞丰县| 宁陕县| 南投市| 安吉县| 义乌市| 阿鲁科尔沁旗| 河津市| 乌拉特后旗| 莱西市| 宁化县| 永春县| 昔阳县| 房产| 沙田区| 百色市| 德化县| 沙雅县| 沁阳市| 永济市| 东安县| 阳高县| 宝兴县| 静乐县| 鹤壁市| 晋州市| 南雄市| 毕节市| 库尔勒市| 同江市| 彭泽县| 茌平县| 香格里拉县|