私は何かについて少し混乱しています。皆さんがこれを明確にすることができれば幸いです. いくつかのメソッドとゲッター/セッターを持つクラス支払いがあります。たとえば、メソッド ItemCost を使用して属性 itemCost のバルブを返すか、それともゲッターを使用するか?
public class Payment {
private int itemCost, totalCost;
public int itemCost(int itemQuantity, int itemPrice){
itemCost = itemPrice * itemQuantity;
return itemCost;
}
public int totalCost(BigDecimal itemPrice){
totalCost = totalCost + itemCost;
return totalCost;
}
public int getBalance(int clickValue, int totalCost){
totalCost = totalCost - clickValue;
return totalCost;
}
public int getTotalcost(){
return this.totalCost;
}
public void setTotalcost(int totalCost){
this.totalCost = totalCost;
}
public int getItemcost(){
return this.itemCost;
}
public void setItemcost(int itemCost){
this.itemCost = itemCost;
}
}
インスタンス化する代わりに: int cost = payment.itemCost(quantity, itemPrice) in another class
DO: payment.itemCost(数量, itemPrice) payment.getItemcost
?
編集 2: すべてのメソッドが void を返すようにし、ゲッターを使用するだけでコーディングが改善されますか?
public class Payment {
private int itemCost, totalCost;
public void calculateItemcost(int itemQuantity, int itemPrice){
itemCost = itemPrice * itemQuantity;
}
public void calculateTotalCost(BigDecimal itemPrice){
this.totalCost = totalCost + itemCost;
}
public void calculateBalance(int clickValue, int totalCost){
this.totalCost = totalCost - clickValue;
}
public int getTotalcost(){
return this.totalCost;
}
public void setTotalcost(int totalCost){
this.totalCost = totalCost;
}
public int getItemcost(){
return this.itemCost;
}
public void setItemcost(int itemCost){
this.itemCost = itemCost;
}
}