gpt4 book ai didi

java - 从父类的对象数组列表中调用子类的方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:31 25 4
gpt4 key购买 nike

我已经搜索过这个问题的解决方案,但我仍然不明白该怎么做。这个程序应该模仿一台自动取款机,它用支票从余额中减去,用存款加钱。

我有这个父类:

public class Transaction
{

private int transNumber;
private int transId;
private double transAmt;

public Transaction(int number, int id, double amount)
{
transNumber = number;
transId = id;
transAmt = amount;
}

public int getTransNumber()
{
return transNumber;
}

public int getTransId()
{
return transId;
}

public double getTransAmount()
{
return transAmt;
}
}

我有一个数组列表 (transList),用于存储每次取款/存款的交易对象。

子类是:

public class Check extends Transaction
{
private int checkNumber; // check number for each check transaction

public Check(int tId, double tAmt, int tCount, int checkNumber) {
super(tCount, tId, tAmt);
this.checkNumber = checkNumber;
}

public int getCheckNumber() {
return checkNumber;
}

public void setCheckNumber(int checkNumber) {
this.checkNumber = checkNumber;
}
}

我需要做的是在 JOptionPane 中打印出用户要求的支票号码。

主要有:

checkNumStr = JOptionPane.showInputDialog("Enter check number:");
checkNum = Integer.parseInt(checkNumStr);
checkAmtStr = JOptionPane.showInputDialog("Eneter check amount:");
checkAmt = Double.parseDouble(checkAmtStr);

Check c = new Check(1, checkAmt, acctCheck.gettransCount(), checkNum);

acctCheck.addTrans(c);

我不确定我是否说得足够清楚。我以前没有在这里问过问题。我需要知道的是如何从父类 Transaction 的对象数组列表中调用 Check.getCheckNumber()。

编辑:transList 位于另一个类:支票账户:

public class CheckingAccount extends Account
{
private double totalServiceCharge;
private static ArrayList<Transaction> transList;
// keeps a list of Transaction objects for the account
private int transCount = 0;
// the count of Transaction objects and used as the ID for each transaction
NumberFormat fmt = NumberFormat.getCurrencyInstance();



public CheckingAccount()
{
transList = new ArrayList<Transaction>();
totalServiceCharge = 0;
}

public double getServiceCharge()
{
return totalServiceCharge;
}

public void setServiceCharge(double currentServiceCharge)
{
totalServiceCharge += currentServiceCharge;
}

// adds a transaction object to the transList
public void addTrans(Transaction newTrans)
{
transList.add (newTrans);
transCount++;
}

//returns the current value of transCount;
public int gettransCount()
{
return transCount;
}
}

最佳答案

在构建类时,您应该考虑has-a is-a 关系。

你的例子,

您有一个 Transaction 类。您有一个 Check 类,它扩展了 Transaction。当您扩展时,您是在说Check is-a Transaction,这是不正确的。但是 Transaction has-a(或有一个)Check 吗?是的!因此,在这种情况下,您可以将 CheckTransaction 的扩展分开,并执行如下操作:

public class Transaction {
Check check; // This is an example of a has-a relationship
}

public class Check { // notice how Check doesn't extend Transaction
// anymore. As it shouldn't, because they don't
} // share any common properties

如果你想要一个 CheckArrayList,我会把它放在 Account 类中,因为 Account does has-a(有一个)checkHistory

public class Account {
ArrayList<Check> checkHistory = new ArrayList<Check>();

public ArrayList<Check> getCheckHistory() {
return checkHistorty;
}
}

因此您可以从 Account 类中调用 checkHistory,这很有意义

Account myAccount = new Account();

ArrayList<Check> list = myAccount.getCheckHistory();

for (Check check : list) {
System.out.print(check.someCheckProperty);
}

关于java - 从父类的对象数组列表中调用子类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19530592/

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