如何覆盖子class中的方法并调用父class方法而不丢失数据
How to override a method in a subclass and call the parent class method without losing data
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
class。说明说我需要覆盖 subclasse 的 "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;
}
}
用户Account.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
class 持有类型 (取款或存款),取款或存款的金额,以及余额。发生的事情是,当我从重写的存款或取款方法中调用超级 class 时,它将余额设置为 0,因此当我想要原始余额和存款时,它说余额是 50、40 或 -5或提取的货币。
如果有人能提供帮助,那就太好了!我可以澄清是否有任何混淆!谢谢!
嘿,我认为您没有在 UserAccount 的构造函数中设置余额。
这里是一个设置变量的例子
假设 class A 为帐户,class B 为用户帐户
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();
}
}
如果您在超级 class 中使用静态平衡,那么它的一个实例将只用于所有对象,因为我猜您希望每个 UserAccount 都有单独的平衡。
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
class。说明说我需要覆盖 subclasse 的 "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;
}
}
用户Account.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
class 持有类型 (取款或存款),取款或存款的金额,以及余额。发生的事情是,当我从重写的存款或取款方法中调用超级 class 时,它将余额设置为 0,因此当我想要原始余额和存款时,它说余额是 50、40 或 -5或提取的货币。
如果有人能提供帮助,那就太好了!我可以澄清是否有任何混淆!谢谢!
嘿,我认为您没有在 UserAccount 的构造函数中设置余额。
这里是一个设置变量的例子
假设 class A 为帐户,class B 为用户帐户
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();
}
}
如果您在超级 class 中使用静态平衡,那么它的一个实例将只用于所有对象,因为我猜您希望每个 UserAccount 都有单独的平衡。