Java ArrayList of ? extends Interface

前端 未结 4 1183
长情又很酷
长情又很酷 2020-12-12 02:13

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--

相关标签:
4条回答
  • 2020-12-12 02:49

    List<? extends Validation> myValidationObjects

    Incorrect reading

    "myValidationObjects is list of objects that extend Validation."

    Correct reading

    "myValidationObjects can be a list of any type that extends Validation. For example, it could be a List<RangeValidation> or a List<RegexValidation>."

    Since there is no object you can legitimately add to both a List<RangeValidation> and a List<RegexValidation>, Java prevents you to call add on a variable of such type.

    Your case is in fact the simpler one: you need the definite type List<Validation>.

    0 讨论(0)
  • 2020-12-12 02:50

    If a generic class's T is <? extends Foo>, then the only thing you can pass to a method that takes T is null -- not any subclass that extends Foo.

    The reason is that List<? extends Validation> doesn't mean "a list of things that extend Validation". You can get that with just List<Validation>. Instead, it means "a list of some type, such that that type extends Validation."

    It's a subtle distinction, but basically the idea is that List<? extends T> is a subtype of List<T>, and you therefore don't want to be able to insert anything into it. Think of this case:

    List<FooValidation> foos = new ArrayList<>();
    List<? extends Validation> validations = foos; // this is allowed
    validations.add(new BarValidation()); // not allowed! this is your question
    FooValidation foo = foos.get(0);
    

    If the third line were allowed, then the last line would throw a ClassCastException.

    0 讨论(0)
  • 2020-12-12 03:02

    The declaration ArrayList<? extends Validation> means a list of an unknown class that extends Validation. Email is not compatible with this unknown class.

    You can use ArrayList<Validation> for your list.

    0 讨论(0)
  • 2020-12-12 03:11

    You can use:

    List<Validation> myValidationObjects = new ArrayList<>();           // Java 7
    List<Validation> myValidationObjects = new ArrayList<Validation>(); // pre Java 7
    

    Now you can add any instance of a class that implements Validation to that list.

    0 讨论(0)
提交回复
热议问题