提问人:Patrick 提问时间:7/25/2015 最后编辑:Patrick 更新时间:7/25/2015 访问量:127
从 setter 设置私有值“balance”时出错,尽管显示值从方法块内部更改
Error setting private value "balance" from setter, despite the showing the value changing from inside method block
问:
制定提款方式,从有余额的活动账户中取款。控制台显示从 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(Double Withdrawal, Double Balance) 从中提款的地方
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 获得的值,因此该值应为 .98:
new balance is: 12.0
如您所见,新余额未在提款方式中设置。有什么想法吗?可能存在按值传递的问题?也许与我的构造函数有关?真的迷路了。必须弄清楚这一点,这样我才能编写其他三种使用平衡的方法。
答:
从方法中删除 balance 参数,并改用类成员 balance。否则,更改将丢失。
您的类几乎没有错误。首先,在您的提款方法中,您有一个余额变量,它隐藏了您的类变量余额。所以如果你想修改类平衡变量,请使用
this.balance = this.balance - withdrawInput;
其次,我看不出有平衡参数的理由,所以如果没有特定的要求,只需将您的方法更改为:
// 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 中删除您的撤回实现(以及任何其他不更改任何内容的方法重新实现),因为这样您就不会从继承中受益。
评论
((SuperClass) this).field