范例(Examples)
下面是Account class的部分代碼:
class Account...
private AccountType _type;
private double _interestRate;
double interestForAmount_days(double amount, int days) {
return _interestRate * amount * days / 365;
}
我想把表示利率的_interestRate搬移到AccountType class去。目前已有數個函數引用了它,interestForAmount_days()就是其一。下一步我要在AccountType中建立_interestRate field以及相應的訪問函數:
class AccountType...
private double _interestRate;
void setInterestRate(double arg) {
_interestRate = arg;
}
double getInterestRate() {
return _interestRate;
}
這時候我可以編譯新的 AccountType class。
現在,我需要讓Account class中訪問_interestRate
field的函數轉而使用AccountType對象,然后刪除Account class中的_interestRate
field。我必須刪除source field,才能保證其訪問函數的確改變了操作對象,因為編譯器會幫我指出未正確獲得修改的函數。
private double _interestRate;
double interestForAmount_days(double amount, int days) {
return _type.getInterestRate() * amount * days / 365;
}