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) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 长治市| 鹤庆县| 泰顺县| 长泰县| 达孜县| 通州市| 札达县| 龙口市| 松滋市| 承德县| 启东市| 哈尔滨市| 茶陵县| 唐河县| 通许县| 无锡市| 涞水县| 晋江市| 巴楚县| 竹北市| 延边| 宁陵县| 高淳县| 禄丰县| 德州市| 仙居县| 固镇县| 六枝特区| 都江堰市| 准格尔旗| 石嘴山市| 炎陵县| 弥勒县| 昌江| 巴里| 南阳市| 临朐县| 济南市| 厦门市| 德兴市| 邹城市|