英文:
How can I modify the parameter of a constructor after it has already been called?
问题
让我们假设我有这个类:
```java
public class Customer {
private double discountAmount;
private double totalPurchases;
public Customer(double tot) {
this.totalPurchases = tot;
if (tot > 2000.00) {
setDiscountAmount(0.25);
} else if (tot >= 1000.00) {
setDiscountAmount(0.15);
} else if (tot >= 500.00) {
setDiscountAmount(0.10);
}
}
public double getDiscountAmount() {
return discountAmount;
}
public double getTotalPurchases() {
return totalPurchases;
}
public void setDiscountAmount(double newDiscountAmount) {
discountAmount = newDiscountAmount;
}
public void setTotalPurchases(double newTotalPurchases) {
totalPurchases = newTotalPurchases;
}
}
而且我想在不创建新对象的情况下更改总购买金额。
import java.util.Scanner;
public class Discounts {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double totalPurchase;
System.out.print("输入顾客消费金额:");
totalPurchase = input.nextDouble();
Customer customer = new Customer(totalPurchase);
System.out.print("\n顾客是否有更多购买? ");
boolean loop = input.nextLine().equalsIgnoreCase("yes");
if (loop) {
System.out.print("购买次数? ");
int x = input.nextInt();
for (int i = 0; i < x; i++) {
// 累积
System.out.print("输入顾客消费金额:");
totalPurchase += input.nextDouble() * customer.getDiscountAmount();
customer.setTotalPurchases(totalPurchase);
}
System.out.println("顾客折扣金额:" + customer.getDiscountAmount());
System.out.println("顾客总购买金额:" + customer.getTotalPurchases());
}
}
}
但是当我打印新的折扣金额时,它仍然保持不变。那么我错在哪里,我该怎么做?
<details>
<summary>英文:</summary>
Let's say I have this class:
public class Customer()
{
private double discountamount;
private double totalPurchase;
public Customer(double tot)
{
this.totalPurchases = tot;
if (tot > 2000.00){setDiscountAmount(0.25);}
else if (tot >= 1000.00){setDiscountAmount(0.15);}
else if (tot >= 500.00){setDiscountAmount(0.10);}
}
public double getDiscountAmount(){return discountAmount;}
public double getTotalPurchases(){return totalPurchases;}
public void setDiscountAmount(double newDiscountAmount){discountAmount = newDiscountAmount;}
public void setTotalPurchases(double newTotalPurchases){totalPurchases = newTotalPurchases;}
}
And I want to change the value of the total purchase without creating a new object.
import java.util.Scanner;
public class Discounts
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double totalPurchase;
System.out.print("Enter amount customer has spent: ");
totalPurchase = input.nextDouble();
Customer customer = new Customer(totalPurchase);
System.out.print("\nHas the customer returned with more purchases? ");
boolean loop = input.nextLine().equalsIgnoreCase("yes");
if (loop){
System.out.print("How many times? ");
int x = input.nextInt();
for (int i = 0; i < x; i++) {
// Accumulate
System.out.print("Enter amount customer has spent: ");
totalPurchase += input.nextDouble() * customer.discountAmount;
customer.setTotalPurchases(totalPurchase);
}
System.out.println("Customer Discount Amount: " + customer.discountAmount);
System.out.println("Customer Total Purchase Amount: " + customer.totalPurchases);
}
}
}
But when I print the new discount amount, it remains unchanged. So where am I going wrong and what can I do?
</details>
# 答案1
**得分**: 2
这是因为 `discountAmount` 只在构造函数中设置,而构造函数仅在实例化对象时执行一次。相反地,你应该将 if 语句从构造函数移到 `setTotalPurchases` 中,就像这样:
```java
public class Customer{
private double discountAmount;
private double totalPurchases;
public Customer(double tot){
this.setTotalPurchases(tot);
}
public double getDiscountAmount(){return discountAmount;}
public double getTotalPurchases(){return totalPurchases;}
public void setDiscountAmount(double newDiscountAmount){discountAmount = newDiscountAmount;}
public void setTotalPurchases(double newTotalPurchases){
totalPurchases = newTotalPurchases;
if (tot > 2000.00){
setDiscountAmount(0.25);
}else if (tot >= 1000.00){
setDiscountAmount(0.15);
}else if (tot >= 500.00){
setDiscountAmount(0.10);
}
}
}
英文:
That's because discountAmount
is only set in the constructor, which happens only once an object is instantiated. Instead, you should move the if-statement in your constructor to the setTotalPurchases
, like so:
public class Customer{
private double discountamount;
private double totalPurchase;
public Customer(double tot){
this.setTotalPurchases(tot);
}
public double getDiscountAmount(){return discountAmount;}
public double getTotalPurchases(){return totalPurchases;}
public void setDiscountAmount(double newDiscountAmount){discountAmount = newDiscountAmount;}
public void setTotalPurchases(double newTotalPurchases){
totalPurchases = newTotalPurchases;
if (tot > 2000.00){
setDiscountAmount(0.25);
}else if (tot >= 1000.00){
setDiscountAmount(0.15);
}else if (tot >= 500.00){
setDiscountAmount(0.10);
}
}
}
答案2
得分: 2
你可以将更改折扣金额的逻辑移到一个setter方法中:
public Customer(double tot) {
setTotalPurchases(tot);
}
public void setTotalPurchases(double newTotalPurchases){
totalPurchases = newTotalPurchases;
if (newTotalPurchases > 2000.00) {
setDiscountAmount(0.25);
} else if (newTotalPurchases >= 1000.00) {
setDiscountAmount(0.15);
} else if (newTotalPurchases >= 500.00) {
setDiscountAmount(0.10);
}
}
英文:
You can move the logic for changing the discount amount to a setter:
public Customer(double tot) {
setTotalPurchases(tot);
}
public void setTotalPurchases(double newTotalPurchases){
totalPurchases = newTotalPurchases;
if (newTotalPurchases > 2000.00) {
setDiscountAmount(0.25);
} else if (newTotalPurchases >= 1000.00) {
setDiscountAmount(0.15);
} else if (newTotalPurchases >= 500.00) {
setDiscountAmount(0.10);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论