在子类的方法中访问抽象超类的ArrayList

时间:2017-12-04 02:53:14

标签: java inheritance arraylist abstract

这只是我代码的一部分。我有这个名为Account的抽象超类,我想在Subclass的方法中访问ArrayList:

public abstract class Account {
    private String accountNumber;
    private ArrayList<String> accountRecords;

    public Account() {
        super();
        accountRecords = new ArrayList<String>();
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public abstract void addTransaction(String transactionInfo);

}


public class SubAccount extends Account {

    public SubAccount() {
        super();
    }

    public void addTransaction(String transaction) {
        if (super.getAccountNumber() != null && 
            !super.getAccountNumber().isEmpty()) {

                accountRecords.add(transaction); //How can I access the 
                                                 ArrayList here?
        }
    }
}

2 个答案:

答案 0 :(得分:0)

为超类中的arraylist编写一个getter并调用super.getArrayList()

答案 1 :(得分:0)

accountRecords的访问权限更改为protected

protected ArrayList<String> accountRecords;