gpt4 book ai didi

java - 如何在仅在单个数组中的 java 中打印多态对象

转载 作者:行者123 更新时间:2023-11-30 11:04:58 25 4
gpt4 key购买 nike

我对如何在继承适用的数组中打印多态对象有疑问。我知道如何将它存储在单个数组中,但在打印对象时,我完全被卡住了。

这是我的主要类(class):

public class AccountArray {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n, i = 0;
int numAcc;
double balance, rate;

Account[] bank = new Account[2];

do {
System.out.print("\n[1]Current || [2]Saving >> ");
n = in.nextInt();
System.out.println("------------");

if (n == 1) {
bank[i] = new Current();
System.out.println("#Current#");
System.out.println("------------");
System.out.print("Please enter account number: ");
numAcc = in.nextInt();
bank[i].setAccountNumber(numAcc);
System.out.print("Enter balance: ");
balance = in.nextDouble();
bank[i].setAccountBalance(balance);
i++;
} else if (n == 2) {
bank[i] = new Saving();

System.out.println("#Saving#");
System.out.println("------------");
System.out.print("Please enter account number: ");
numAcc = in.nextInt();
bank[i].setAccountNumber(numAcc);
System.out.print("Enter balance: ");
balance = in.nextDouble();
bank[i].setAccountBalance(balance);
System.out.print("Interest rate: ");
rate = in.nextDouble();
((Saving) bank[i]).setInterest(rate);
System.out.println("---------------------------------------");
i++;
}
} while (i < bank.length);

//Output
System.out.println("### Data Output ###");
for (int d = 0; d < bank.length; d++) {

System.out.println("--------------------");
System.out.println("Account Number: \t" + bank[d].getAccountNumber());
System.out.println("Balance : \t" + bank[d].getAccountBalance());
if (bank[d] == new Saving()) {
System.out.println("Interest rate : " + ((Saving) bank[d]).getInterest());
}
}
}

这是我的带有抽象类的帐户类:

public abstract class Account {

private int numAcc;
private double balance;

public Account() {

}

public Account(int numAcc, double balance) {
this.numAcc = numAcc;
this.balance = balance;
}

public int getAccountNumber() {
return numAcc;
}

public void setAccountNumber(int numAcc) {
this.numAcc = numAcc;
}

public double getAccountBalance() {
return balance;
}

public void setAccountBalance(double balance) {
this.balance = balance;
}

public abstract void display();
}

这是储蓄类(Class)

public class Saving extends Account {

private double interest;

public Saving(int numAcc, double balance, double interest) {
super(numAcc, balance);
this.interest = interest;
}

public Saving() {

}

public void setInterest(double interest) {

this.interest = interest;
}

public double getInterest() {

return interest;
}

@Override
public void display() {
System.out.println("Saving Account Information");
System.out.println("account number: " + getAccountNumber());
System.out.println("balance: RM " + getAccountBalance());
System.out.println("interest: " + getInterest() + " %");
}
}

这是我现在的类(class):

public class Current extends Account {

public Current(int accNum, double balance) {
super(accNum, balance);
}

public Current() {

}

@Override
public void display() {
System.out.println("Current Account Information");
System.out.println("account number: " + getAccountNumber());
System.out.println("balance: RM " + getAccountBalance());
}

}

最佳答案

另一种方法是覆盖每个对象的toString() 方法。

关于java - 如何在仅在单个数组中的 java 中打印多态对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29821723/

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