I have a group of classes that all implement a validation interface which has the method isValid()
. I want to put a group of objects--all of different classes--into an ArrayList, loop through them and call isValid()
on each.
Here's my code
Email email = new email();
Address address = new Address();
ArrayList<? extends Validation> myValidationObjects = new ArrayList();
But when I try to do:
myValidationObjects.add(email);
I get:
The method add(capture#2-of ? extends Validation) in the type ArrayList
is not applicable for the arguments (Email)
Both Email
and Address
implement Validation.
According to this document, I should be able to use extends
for both interfaces and subclasses.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…