posts - 431,  comments - 344,  trackbacks - 0

          在Java1.4及以前,子類方法如果要覆蓋超類的某個(gè)方法,必須具有完全相同的方法簽名,包括返回值也必須完全一樣。
          Java5.0放寬了這一限制,只要子類方法與超類方法具有相同的方法簽名,或者子類方法的返回值是超類方法的子類型,就可以覆蓋。
          注意:"協(xié)變返回(covariant return)",僅在subclass的返回類型是superclass返回類型的extension時(shí)才被容許。
           

          package com.eric.news;

          public 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;
           }
           
          }


          package com.eric.news;

          public 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;
           }
          }



          package com.eric.news;

          public 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;
           }
          }


          package com.eric.news;

          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;
           }
          }

          posted on 2008-04-22 22:45 周銳 閱讀(289) 評(píng)論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 凉城县| 玛多县| 竹溪县| 乐清市| 平果县| 宁远县| 宜兴市| 北流市| 新绛县| 富蕴县| 信阳市| 尉犁县| 高碑店市| 永靖县| 抚宁县| 平阴县| 蒲江县| 西贡区| 宿州市| 丹东市| 海门市| 大港区| 延长县| 靖边县| 郴州市| 玉山县| 娄烦县| 商城县| 宕昌县| 怀宁县| 襄垣县| 卫辉市| 麻阳| 泰安市| 藁城市| 无棣县| 古蔺县| 张家口市| 垦利县| 米易县| 石嘴山市|