当我在 Setters 中只有 if/else 条件时,该程序无法运行。我得到一个提示,我也必须在构造函数中使用它们。有人可以向我解释..为什么?
另一个问题:您是否将 if/else 语句放在构造函数或设置器中?
//构造函数
public Invoice(String partNumber, String partDescription, int quantity,
double pricePerItem) {
super();
this.partNumber = partNumber;
this.partDescription = partDescription;
if (quantity <= 0)
quantity = 0;
else
this.quantity = quantity;
if (pricePerItem <= 0)
pricePerItem = 0.0;
else
this.pricePerItem = pricePerItem;
}
//二传手
public void setQuantity(int quantity) {
if (quantity <= 0)
this.quantity = 0;
else
this.quantity = quantity;
}
public double getPricePerItem() {
return pricePerItem;
}
public void setPricePerItem(double pricePerItem) {
if (pricePerItem != 0.0)
this.pricePerItem = 0.0;
else
this.pricePerItem = pricePerItem;
}