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學習筆記
          主站蜘蛛池模板: 乌兰察布市| 加查县| 色达县| 祁连县| 铜川市| 库伦旗| 双辽市| 霍林郭勒市| 益阳市| 德保县| 凤台县| 涞源县| 揭阳市| 永修县| 庄浪县| 正阳县| 平安县| 天台县| 邮箱| 兖州市| 中西区| 汨罗市| 潼关县| 梨树县| 明光市| 固原市| 昌吉市| 土默特左旗| 桓台县| 巴南区| 得荣县| 永靖县| 都安| 临夏市| 岫岩| 石阡县| 应城市| 呼玛县| 体育| 余庆县| 崇明县|