我在将类型为Account的变量c1和c2添加到变量名称为sort的CustomerAccount类型的ArrayList时遇到问题。问题指出将c1和c2分配给Account类型,并将ArrayList排序设置为CustomerAccount类型。 CustomerAccount和Transaction都是Account类的子类。

在main方法的for循环下面,我尝试执行以下操作:

sort.add(c1);


但是收到以下错误。

The method add(CustomerAccount) in the type ArrayList<CustomerAccount> is not applicable for the arguments (Account)


CustomerAccount.java:

package questionOne;

import java.util.ArrayList;
//import java.util.Arrays.sort;
import java.util.Date;

public class CustomerAccount extends Account {

public static String name;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ArrayList<Transaction> transactions = new ArrayList<Transaction>();
    CustomerAccount george = new CustomerAccount("George", 1122, 1000.0);
    george.setannualInterest(1.5);
    Account c1 = george;

    george.deposit(30);
    george.deposit(40);
    george.deposit(50);

    george.withdraw(5);
    george.withdraw(4);
    george.withdraw(2);

    System.out.println(c1.toString());

    transactions = george.getTransactions();
    for (int i=0; i < transactions.size(); i++) {
        System.out.print("type: " + (transactions.get(i).getType()));
        System.out.print(" amount: " + (transactions.get(i).getAmount()));
        System.out.print(" balance: " + (transactions.get(i).getBalance()));
        System.out.print((transactions.get(i).getDescription()));
        System.out.println("");
    }

    CustomerAccount john = new CustomerAccount("John", 1123, 500);
    john.setannualInterest(2.5);
    Account c2 = john;

    ArrayList<CustomerAccount> sort = new ArrayList<CustomerAccount>();
    sort.add((CustomerAccount) c1);
    sort.add(c2);

}

CustomerAccount(String name, int id, double balance) {
    super(id, balance);
    this.name = name;
}

}

class Transaction extends Account {
    private java.util.Date dateCreated;
    private char type;
    private double amount;
    private double balance;
    private String description;
    private double transaction;

Transaction() {

}

Transaction(char type, double amount, double balance, String description) {
    dateCreated = new java.util.Date();
    this.type = type;
    this.amount = amount;
    this.balance = balance;
    this.description = description;
}

public String getDateCreated() {
    return dateCreated.toString();
}

public char getType() {
    return type;
}

public double getAmount() {
    return amount;
}

public double getBalance() {
    return balance;
}

public String getDescription() {
    return description;
}

public double getTransaction() {
    return this.transaction;
}

public void setType(char type) {
    this.type = type;
}

public void setAmount(double amount) {
    this.amount = amount;
}

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

public void setDescription(String description) {
    this.description = description;
}

public void setTransaction(double amount) {
    this.transaction = amount;
}
}


帐户类别:

class Account {
    protected int id = 0;
    protected double balance = 0.0;
    protected double annualInterestRate = 0.0;
    private java.util.Date dateCreated;
    ArrayList<Transaction> transactions = new ArrayList<Transaction>();

// set dateCreated for the time and date the account was created
Account() {
    dateCreated = new java.util.Date();
}

// Set accounts id and balance equal to this objects
Account(int id, double balance){
    this();
    this.id = id;
    this.balance = balance;
}

// Getters and Setters manipulating variables to be set to the created account
// Setters have no return value and set itself equal to the variable and getters
// grab the variables and return them.
public int getId() {
    return this.id;
}

public double getBalance() {
    return this.balance;
}

public double getannualInterestRate() {
    return this.annualInterestRate;
}

public String getDateCreated() {
    return this.dateCreated.toString();
}

public void setId(int id) {
    this.id = id;
}

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

public void setannualInterest(double annualInterestRate) {
    this.annualInterestRate = annualInterestRate;
}

public double getMonthlyInterestRate() {
    return (annualInterestRate / 100) / 12;
}

public double getMonthlyInterest() {
    return balance * getMonthlyInterestRate();
}

// set balance of withdraw to balance - amount = balance
public void withdraw (double amount) {
    if(amount < balance) {
        balance -= amount;
        transactions.add(new Transaction('W', amount, balance, " withdrawal "));
    }
}

// set balance of deposit to balance + amount = balance
public void deposit(double amount) {
    balance += amount;
    transactions.add(new Transaction('D', amount, balance, " deposit "));
}

public ArrayList<Transaction> getTransactions() {
    return transactions;
}

@Override
public String toString() {
    return "Account holder name: " + CustomerAccount.name +
            "\nAccount id: " + id +
            "\nAnnual interest: " + this.getannualInterestRate() +
            "\nMonthly Interest Rate: " + this.getMonthlyInterestRate() +
            "\nBalance " + this.getBalance() +
            "\nTransaction: ";
}
}

最佳答案

您的ArrayList存储CustomerAccountc1Account类型,因此不能添加到列表中。

如果相反,它将起作用,因为CustomerAccount扩展了Account

CustomerAccount george = new CustomerAccount("George", 1122, 1000.0);
ArrayList<Account> sort = new ArrayList<>();
sort.add(george);

07-25 21:37