尽管显示从方法块内部更改的值,但从 setter 设置私有值“余额”时出错

Posted

技术标签:

【中文标题】尽管显示从方法块内部更改的值,但从 setter 设置私有值“余额”时出错【英文标题】:Error setting private value "balance" from setter, despite the showing the value changing from inside method block 【发布时间】:2015-10-15 13:52:52 【问题描述】:

制作提款方法以从有余额的活动账户中提款。控制台显示了从 TestClass 正确传递到定义提款方法的帐户类的提款值。它还显示了“余额”的值在提款方法中的变化。

但是,当我在下一行代码中调用余额值时,它会给出我创建帐户时的初始余额值。

我将包括看起来相关的内容:

这是超类

public class Account 
//declares the variables
private double initialBalance;
private double credit;
private double balance;

//default constructor
public Account() 
    this(0.0, 0.0);


//constructs Account object
public Account(double initialBalance, double balance) 
        this.initialBalance = initialBalance;
        this.balance = initialBalance;


//gets initial balance
public double getInitialBalance() 
    return initialBalance;


//gets balance
public double getBalance() 
    return balance;


// Sets initial balance
public void setInitialBalance(double initialBalance) 
    if (initialBalance > 0)
        this.initialBalance = initialBalance;
        this.balance = initialBalance;
    
    else noCrediting();


//crediting the account if the credit function gets a positive input
public double credit(double creditInput, double balance)
    if (creditInput>0)
    
        balance = balance + creditInput;
       
    else
        noCrediting();
    return balance;



//tells the user no credit was added
public void noCrediting() 
    System.out.println("No credit was added because the deposit was not a positive double.");


//withdrawing from the account if the function gets a positive input
public void withdraw(double withdrawInput, double balance)
    if (withdrawInput>0 && withdrawInput<balance)
    
        balance = balance - withdrawInput;
    
    else
        noWithdrawing();


//tells the user no withdrawal was performed
public void noWithdrawing() 
    System.out.println("No amount was withdrawn because the deposit was not a positive double less than the balance.");

这是子类

public class CheckingAccount extends Account 

//declares the variables
private double feeChargedPerTransaction = 2.0;

//default constructor
public CheckingAccount() 
    this(0.0, 0.0, 2.0);


//constructs Account object
public CheckingAccount(double initialBalance, double balance, double feeChargedPerTransaction) 
    super(initialBalance,  balance);
    feeChargedPerTransaction = 2.0;


//withdrawing from the account if the function gets a positive input
public void withdraw(double withdrawInput, double balance)
    if (withdrawInput>0 && withdrawInput < balance)
    
        System.out.println("The withdrawal amount from the checking account class is showing"+ withdrawInput); 
        balance = (balance - withdrawInput)*.98;
        System.out.println("Now The balance from the checking account class is showing: " + balance);

    
    else
        noWithdrawing();


//gets fee
public double getFee() 
    return feeChargedPerTransaction;


public void noWithdrawing() 
    System.out.println("No amount was withdrawnnn because the deposit was not a positive double less than the balance.");

这里是被调用的方法

//withdrawing from the account if the function gets a positive input
public void withdraw(double withdrawInput, double balance) 
    if (withdrawInput>0 && withdrawInput < balance) 
        System.out.println("The withdrawal amount from the checking account class is showing"+ withdrawInput); 
        balance = (balance - withdrawInput)*.98;
        System.out.println("Now The balance from the checking account class is showing: " + balance);
    
    else
        noWithdrawing();

我已将打印添加到控制台以跟踪正在发生的事情,因为它都是通过我的 testClass 中的 javaFX 打印的

String test = "current balance is: "
    + findCheckingAccountIndex(s, checkingsArray, nameListChecking).getBalance();
System.out.println(test);
test = "withdraw amount: " + Double.parseDouble(transactionField.getText());
System.out.println(test);

这里是它找到帐户并使用withdraw(双重提款,双重余额)从中提取的地方

findCheckingAccountIndex(s, checkingsArray,           nameListChecking).withdraw(Double.parseDouble(transactionField.getText()),findCheckingAccountIndex(s, checkingsArray, nameListChecking).getBalance());

这是 getter 应该显示变化的地方!

test = "new balance is: " + findCheckingAccountIndex(s,checkingsArray,nameListChecking).getBalance();
    System.out.println(test);

现在假设我将帐户设为 12 美元。我输入了 11 的提款金额,您会发现:

从测试类打印显示即将通过的提款以及从获得余额中获得的价值:

current balance is: 12.0   
withdraw amount: 11.0

在执行方法块时从相关子类打印:

The withdrawal amount from the checking account class is showing 11.0
Now The balance from the checking account class is showing: 0.98

太棒了!现在显示从在withdraw方法之后调用的getter获得的值,因此该值应该是0.98:

new balance is: 12.0

如您所见,新余额并未在提款方法中设置。有任何想法吗?可能的价值传递问题?也许与我的构造函数有关?真的输了。必须弄清楚这一点,这样我才能编写其他三种使用平衡的方法。

【问题讨论】:

Java 不支持按引用传递。 你能告诉我们你的班级结构吗?我的第一个想法是你在超类中定义了一个私有字段,并在你的子类中用同名字段隐藏了它。 @Peter Lawrey 将其放入。我的 testClass 中还有一个名为 balance 的变量,但该函数正在调用特定于存储在 arrayList 中的对象的 getter,所以我认为这应该不是问题?我确实知道该函数正在调用我想要的子类中的方法。也许我也需要从那里显式调用 getter 方法? 方法可以被覆盖,即被子类中的方法替换,但字段是隐藏的,并且它们都存在于子类中。您可以使用((SuperClass) this).field 更加明确,但最好为该字段指定一个不同的名称以避免混淆。 【参考方案1】:

从方法中删除 balance 参数并使用类成员 balance 代替。否则更改将丢失。

【讨论】:

【参考方案2】:

您的课程几乎没有错误。首先,在您的提款方法中,您有一个 balance 变量,它隐藏了您的类变量 balance。所以如果你想修改类平衡变量使用

this.balance = this.balance - withdrawInput;

其次,我完全没有理由使用 balance 参数,所以如果没有特定要求,只需将您的方法更改为:

// withdrawing from the account if the function gets a positive input
public void withdraw(double withdrawInput) 
    if (withdrawInput > 0 && withdrawInput < balance) 
        balance = balance - withdrawInput;
     else
        noWithdrawing();

应该可以/

最后但并非最不重要的一点是,在 CheckingAccount 中删除您的撤销实现(以及任何其他不会改变任何内容的方法重新实现),因为这样您就不会从继承中受益。

【讨论】:

以上是关于尽管显示从方法块内部更改的值,但从 setter 设置私有值“余额”时出错的主要内容,如果未能解决你的问题,请参考以下文章

为啥尽管源代码没有变化,但从一个系统到另一个系统的片段数量却有很大差异?

如何从 X 列中选择唯一列,但从 SQL 的结果中显示 X + Y 列?

尽管链接到带有导​​出符号的 .lib 文件,但从托管 C++ 链接到非托管 C++ 时出现链接错误

static

static

使用滑块更改 Phong 着色器的值