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 閱讀(516) 評論(0)  編輯  收藏 所屬分類: Tiger學習筆記
          主站蜘蛛池模板: 张家界市| 商南县| 余干县| 乐山市| 永和县| 和政县| 绥德县| 西峡县| 南木林县| 敦化市| 万源市| 区。| 惠州市| 洪雅县| 宁明县| 克拉玛依市| 巴中市| 津市市| 深圳市| 临泉县| 锡林浩特市| 江山市| 肇东市| 弥渡县| 常德市| 汾阳市| 布拖县| 姜堰市| 怀安县| 扬中市| 大英县| 长葛市| 广宗县| 高唐县| 榆社县| 浦江县| 安化县| 南充市| 祁连县| 乌兰县| 泾阳县|