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 周銳 閱讀(298) 評(píng)論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 双柏县| 庄河市| 大同县| 广安市| 开阳县| 衡山县| 象州县| 巨鹿县| 仪征市| 瓦房店市| 吉安县| 老河口市| 陇西县| 永城市| 乐清市| 孟连| 冷水江市| 浦江县| 泽库县| 富宁县| 珲春市| 阿瓦提县| 景德镇市| 阿克陶县| 阜南县| 叶城县| 曲阳县| 南乐县| 共和县| 镇江市| 邓州市| 禄丰县| 桃园县| 山阴县| 宝兴县| 凤山市| 富平县| 织金县| 乐山市| 韶关市| 襄城县|