gpt4 book ai didi

java - 如何重写子类中的方法并调用父类方法而不丢失数据

转载 作者:太空宇宙 更新时间:2023-11-04 10:07:03 25 4
gpt4 key购买 nike

This is only the second time I use this, and the first time I got really good help so I am hoping I could get some more help!

背景:

我的程序通过创建一个保存余额和其他内容的对象来测试我的 Account 类。说明说我需要重写子类的“Withdraw”“Deposit”方法才能跟踪交易。

我这样做了,但它不使用当前余额,而只是发送 0。我希望能够保留新余额,以便它实际上从当前余额中提取或存款。

抱歉,如果这没有意义,如果需要任何澄清,我会尝试以不同的方式进行解释。

下面是我的代码片段:

Note: I kept out the stuff that already works (constructors and other methods) that has nothing to do with this:

Account.java

public class Account
{
private static double balance;

public void Withdraw(double withdrawAmount)
{
this.balance -= withdrawAmount;
}
public void Deposit(double depositAmount)
{
this.balance += depositAmount;
}
}

UserAccount.java

public class UserAccount extends Account
{
private ArrayList<Transactions> transactions = new ArrayList();
@Override
public void Withdraw(double withdrawAmount)
{
super.Withdraw(withdrawAmount);
Transactions thisTransaction = new Transactions('W',
withdrawAmount, this.AccBalance(), "Withdraw");
this.transactions.add(thisTransaction);
}
@Override
public void Deposit(double depositAmount)
{
super.Deposit(depositAmount);
Transactions thisTransaction = new Transactions('D',
depositAmount, this.AccBalance(), "Deposit");
this.transactions.add(thisTransaction);
}
public void fillTransactions(char type, double amount, double balance)
{
switch (type) {
case 'D':
this.Deposit(amount);
break;
case 'W':
this.Withdraw(amount);
break;
default:
System.out.println("ERROR fillTransactions");
break;
}
}
public static void main(String[] args) {
ArrayList<Transactions> copyTransactions;
UserAccount thisAccount = new UserAccount("George", 1122, 1000);
thisAccount.MutAIR(.015);

thisAccount.fillTransactions('D', 30, thisAccount.AccBalance());
thisAccount.fillTransactions('D', 40, thisAccount.AccBalance());
thisAccount.fillTransactions('D', 50, thisAccount.AccBalance());
thisAccount.fillTransactions('W', 5, thisAccount.AccBalance());
thisAccount.fillTransactions('W', 4, thisAccount.AccBalance());
thisAccount.fillTransactions('W', 2, thisAccount.AccBalance());
}

我拥有的 Transactions 类保存类型(取款或存款)、取款或存款的金额以及余额。发生的情况是,当我从重写的存款或取款方法中调用父类(super class)时,它将余额设置为 0,因此当我想要存款或取款货币的原始余额时,它表示余额为 50、40 或 -5。

如果有人能提供帮助,那就太棒了!如果有什么令人困惑的地方我可以澄清!谢谢!

最佳答案

嘿,我认为您没有在 UserAccount 的构造函数中设置余额。
这是设置变量的示例假设 A 类为 Account,B 类为 UserAccount

class A {
private double balance;// Don't use static
public A(double balance) {// You will need this
this.balance = balance;
}
public double getBalance() { return balance; }
}
class B extends A {
public B(double balance) {
super(balance);//Important
}
public void d() { System.out.println(getBalance()); }
}
public class Main {
public static void main(String args[]) {
B b = new B(100.0);
b.d();
}
}

如果您在父类(super class)中使用静态平衡,那么只有它的一个实例将用于所有对象,因为我猜您希望为每个 UserAccount 提供单独的平衡。

关于java - 如何重写子类中的方法并调用父类方法而不丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52789661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com