문제

I've got an abstract class called customer and another classe called payCust that extends this abstract class. There is another client class that initializes a list of customers.List<Customer> custList = new ArrayList<>();

the customer class has a constructor as follows:

public Customer(String name, String email, String mag) {
        this.CusEmail = email;
        this.CusName = name;
        this.magazine = mag;
    }

payCust has the following constructor:

public PayCust(String _name, String _email, String _accountType, String _custMag) {
        super(_name, _email, _custMag);
        this.accountType = _accountType;
    }

all the variables have public get and set methods. e.g.

public void setName(String name) {
            this.CusName = name;
        }
public String getName() {
        return this.CusName;
    }

my question is that if the custList had a PayCust added to it. how can i edit the accountType of a customer from that list? note: email is unique to every customer

도움이 되었습니까?

해결책

You will have to check the instance type of the object within the ArrayList and cast it for usage.

Something like this, for example:

for (Customer c : custList){
    if(c instanceof PayCust){
        PayCust pc = (PayCust) c;
        pc.getAccountType();
    }
}

다른 팁

You would have to cast it to a PayCust (assuming you know for a fact that it's a PayCust):

PayCust example = (PayCust) custList.get(0);
String accountType = example.getAccountType ();
...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top