【问题标题】:How can I modify the parameter of a constructor after it has already been called?如何在构造函数被调用后修改其参数?
【发布时间】:2021-02-02 16:56:03
【问题描述】:

假设我有这个课程:

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;}

}

我想在不创建新对象的情况下更改总购买的价值。

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);
}
}
}

但是当我打印新的折扣金额时,它保持不变。那么我哪里出错了,我该怎么办?

【问题讨论】:

  • 您永远不会更改折扣金额,只会更改总购买金额。
  • 我将如何更改折扣金额?如果获取和设置在那里并且我将逻辑从构造函数移动到setTotalPurchases

标签: java class inheritance methods


【解决方案1】:

这是因为discountAmount 仅在构造函数中设置,仅在实例化对象时才会发生。相反,您应该将构造函数中的 if 语句移动到 setTotalPurchases,如下所示:

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);
         }
    }

}

【讨论】:

  • 好吧,看,我还是没有gets和sets的来龙去脉。但这清楚地说明了这些是如何工作的。谢谢。
  • 请稍等,现在我的折扣金额为零。我可以在主要或toString 上说错了吗?
【解决方案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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    相关资源
    最近更新 更多