lotusswan

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            11 Posts :: 0 Stories :: 4 Comments :: 0 Trackbacks

          為了運用多態性,當編寫子類時,你肯定會覆寫超類的方法。當你覆寫超類的方法時,你是否想到過更改方法的返回類型。如果你沒有,那祝賀你,你不曾為此煩惱過;如果你想到過,那你肯定是非常的郁悶。在Java 5.0以前,你是無法更改超類定義的方法的返回類型的。不過當你使用Java 5.0以后,你會驚喜地發現,此煩惱不復存在了。

          下面的示例顯示了這一新特性的用法:

          package ?com.jiang.tiger.chap1;

          class ?Point2D? {
          ??????
          protected ? int ?x,?y;

          ??????
          public ?Point2D(?)? {
          ????????
          this .x = 0 ;
          ????????
          this .y = 0 ;
          ??????}

          ??
          ??????
          public ?Point2D( int ?x,? int ?y)? {
          ????????
          this .x? = ?x;
          ????????
          this .y? = ?y;
          ??????}

          ??????
          ??????
          public ?String?toString()? {
          ??????????
          return ? " the?position?is?x?=? " ? + ?x? + ? " ,y?=? " ? + ?y?;
          ??????}

          ????}


          ????
          class ?Point3D? extends ?Point2D? {
          ??????
          protected ? int ?z;

          ??????
          public ?Point3D( int ?x,? int ?y)? {
          ????????
          this (x,?y,? 0 );
          ??????}

          ?
          ??????
          public ?Point3D( int ?x,? int ?y,? int ?z)? {
          ????????
          this .x? = ?x;
          ????????
          this .y? = ?y;
          ????????
          this .z? = ?z;?
          ??????}

          ??????
          ??????
          public ?String?toString()? {
          ??????????
          return ? " the?position?is?x?=? " ? + ?x? + ? " ,y?=? " ? + ?y? + ? " ,z=? " ? + ?z;
          ??????}

          ????}


          ????
          class ?Position2D? {
          ??????Point2D?location;
          ?
          ??????
          public ?Position2D(?)? {
          ????????
          this .location? = ? new ?Point2D(?);
          ??????}


          ??????
          public ?Position2D( int ?x,? int ?y)? {
          ????????
          this .location? = ? new ?Point2D(x,?y);
          ??????}


          ??????
          public ?Point2D?getLocation(?)? {
          ????????
          return ?location;
          ??????}

          ????}


          ????
          public ? class ?Position3D? extends ?Position2D? {
          ??????Point3D?location;
          ?
          ??????
          public ?Position3D( int ?x,? int ?y,? int ?z)? {
          ????????
          this .location? = ? new ?Point3D(x,?y,?z);
          ??????}


          ??????
          public ?Point3D?getLocation(?)? {
          ????????
          return ?location;
          ??????}

          ????????????
          ??????
          public ? static ? void ?main(String[]?args)? {
          ??????????Position2D?position?
          = ? new ?Position3D( 12 ,? 23 ,? 36 );
          ??????????Point3D?clone?
          = ?(Point3D)position.getLocation();
          ??????????System.out.println(clone);
          ??????}

          ????}

          為驗證功能是否正確,我們看看運行結果:

          the position is x = 12,y = 23,z= 36

          posted on 2006-11-26 23:42 lotusswan 閱讀(508) 評論(0)  編輯  收藏 所屬分類: Tiger學習筆記
          主站蜘蛛池模板: 香港| 苗栗市| 县级市| 宁远县| 武义县| 普定县| 昭通市| 错那县| 炎陵县| 桃源县| 襄城县| 赤峰市| 遂溪县| 建德市| 甘谷县| 德兴市| 凉山| 射洪县| 广宗县| 大姚县| 江源县| 鹤山市| 玛多县| 二手房| 平南县| 万宁市| 伊宁市| 徐汇区| 成武县| 铜陵市| 富顺县| 改则县| 金川县| 宝丰县| 全南县| 海安县| 印江| 龙陵县| 新沂市| 长海县| 贡觉县|