Java ArrayList of ? extends Interface

前端 未结 4 1193
长情又很酷
长情又很酷 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 03:11

    You can use:

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

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

提交回复
热议问题