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)編輯 收藏

          僅列出標題  
          主站蜘蛛池模板: 万山特区| 大田县| 综艺| 泗水县| 甘谷县| 黑龙江省| 格尔木市| 九龙城区| 竹山县| 涪陵区| 淮阳县| 张家川| 宁陵县| 正宁县| 深泽县| 嘉荫县| 怀柔区| 鄂尔多斯市| 丹寨县| 平远县| 罗江县| 潼南县| 马鞍山市| 迁西县| 攀枝花市| 铜梁县| 沈阳市| 柳江县| 绥中县| 万源市| 鄂伦春自治旗| 汪清县| 韶山市| 台中市| 海城市| 连州市| 吐鲁番市| 万载县| 峨眉山市| 昭苏县| 贺兰县|