Java ArrayList of ? extends Interface

前端 未结 4 1195
长情又很酷
长情又很酷 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 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 or a List."

    Since there is no object you can legitimately add to both a List and a List, 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.

提交回复
热议问题