范例(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去。目前已有數(shù)個(gè)函數(shù)引用了它,interestForAmount_days()就是其一。下一步我要在AccountType中建立_interestRate field以及相應(yīng)的訪問(wèn)函數(shù):
class AccountType...
private double _interestRate;
void setInterestRate(double arg) {
_interestRate = arg;
}
double getInterestRate() {
return _interestRate;
}
這時(shí)候我可以編譯新的 AccountType class。
現(xiàn)在,我需要讓Account class中訪問(wèn)_interestRate field的函數(shù)轉(zhuǎn)而使用AccountType對(duì)象,然后刪除Account class中的_interestRate field。我必須刪除source field,才能保證其訪問(wèn)函數(shù)的確改變了操作對(duì)象,因?yàn)榫幾g器會(huì)幫我指出未正確獲得修改的函數(shù)。
private double _interestRate;
double interestForAmount_days(double amount, int days) {
return _type.getInterestRate() * amount * days / 365;
}
下面是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去。目前已有數(shù)個(gè)函數(shù)引用了它,interestForAmount_days()就是其一。下一步我要在AccountType中建立_interestRate field以及相應(yīng)的訪問(wèn)函數(shù):
class AccountType...
private double _interestRate;
void setInterestRate(double arg) {
_interestRate = arg;
}
double getInterestRate() {
return _interestRate;
}
這時(shí)候我可以編譯新的 AccountType class。
現(xiàn)在,我需要讓Account class中訪問(wèn)_interestRate field的函數(shù)轉(zhuǎn)而使用AccountType對(duì)象,然后刪除Account class中的_interestRate field。我必須刪除source field,才能保證其訪問(wèn)函數(shù)的確改變了操作對(duì)象,因?yàn)榫幾g器會(huì)幫我指出未正確獲得修改的函數(shù)。
private double _interestRate;
double interestForAmount_days(double amount, int days) {
return _type.getInterestRate() * amount * days / 365;
}