代码之家  ›  专栏  ›  技术社区  ›  sworwitz

如何避免代码重复,通过多态性保持信息隐藏[关闭]

  •  -1
  • sworwitz  · 技术社区  · 7 年前

    我编写了以下两个类(实现状态模式):

    价格:

    public abstract class Price {
    
    
        public abstract double getAmount(int rentalDays);
    
        public abstract  int getPriceCode();
    
    }
    

    正常价格:

    public class RegularPrice extends Price {
    
        private static final int _priceCode = 0;
    
    
        @Override
        public double getAmount(int rentalDays) {
            double res = 2;
            if (rentalDays > 2)
                res += (rentalDays - 2) * 1.5;
            return res;
        }
    
        @Override
        public int getPriceCode() {
            return _priceCode;
        }
    }
    

    问题是添加其他具有差异的价格子类 _priceCode 转换为 getPriceCode() 方法 我曾想过将该方法拉到超类,但后来我无法声明 _价格代码 私有的

    标准溶液是什么?

    4 回复  |  直到 7 年前
        1
  •  1
  •   Jul10    7 年前

    我看不出有什么原因 getPriceCode() 必须留在派生类中,因为它是所有子类的常见行为,所以将其作为基类。所以 _priceCode 在中 Price 未分配,应声明为 受保护的 包裹 (取决于您是否希望仅从同一个包中的子类,甚至是从属于另一个包的子类自由访问它)。

    价格

    public abstract class Price {
        protected static final int _priceCode;    // do you really need static?
    
        Price(int p){ this._priceCode=p;}
        public abstract double getAmount(int rentalDays);
        public int getPriceCode(){ return _priceCode; }
    
    }
    

    正常价格

    public class RegularPrice extends Price {
    
         RegularPrice(int p){ 
             //many ways to do this, but use costructors to set members
             super(p); 
          } 
    
        public double getAmount(int rentalDays) {
            double res = 2;
            if (rentalDays > 2)
                res += (rentalDays - 2) * 1.5;
            return res;
        }
    
    }
    

    这样, _价格代码 直接地 仅从的子类可见 价格 ,从代码的其他部分只能使用getter方法和 getPriceCode() 不重复。

        2
  •  1
  •   robertf    7 年前

    这里有几个选项:

    1) 删除“private”关键字,使其成为“package”级访问。

    2) 向超类中添加getter和/或setter,以提供所需的访问类型。

        3
  •  -1
  •   mckuok    7 年前

    也许你可以试试这种方法。

    public abstract class Price {
        protected final int _priceCode;
    
        public Price(int priceCode) {
            this._priceCode = priceCode;
        }
    
        public final int getPriceCode() {
            return this._priceCode;
        }
    
        public abstract double getAmount(int rentalDays);
    }
    
    public class RegularPrice extends Price {
    
        public RegularPrice() {
            super(0);  // put the default priceCode here, or use a private final static int PRICE_CODE = 0 to avoid magic number
        }
    
        @Override
        public double getAmount(int rentalDays) {
            double res = 2;
            if (rentalDays > 2)
                res += (rentalDays - 2) * 1.5;
            return res;
        }
    
    }
    
        4
  •  -1
  •   Yoghi    7 年前
    public class Price {
       private int _priceCode;
       public double getAmount(int rentalDays);
    
       public int getPriceCode() {
        return _priceCode;
       }
    }
    

    在子类上执行此操作:

    public class RegularPrice extends Price {
    
      private int _priceCode = 0;
    
    
      @Override
      public double getAmount(int rentalDays) {
        double res = 2;
        if (rentalDays > 2)
            res += (rentalDays - 2) * 1.5;
        return res;
      }
    
      @Override
      public void setPriceCode(int priceCode) {
        _priceCode = priceCode;
      }
    }