Java using generics with lists and interfaces

后端 未结 6 1783
遇见更好的自我
遇见更好的自我 2021-01-13 11:02

Ok, so here is my problem:

I have a list containing interfaces - List a - and a list of interfaces that extend that interface: Li

6条回答
  •  甜味超标
    2021-01-13 11:52

    There's no way to do it that is type safe.

    A List cannot have arbitrary Interfaces added to it. It is a list of SubInterfaces after all.

    If you are convinced that this is safe to do even though it is not type safe you can do

    @SuppressWarnings("unchecked")
    void yourMethodName() {
      ...
      List a = (List) b;
      ...
    }
    

提交回复
热议问题