List<? extends MyType>

后端 未结 5 1879
旧时难觅i
旧时难觅i 2020-11-27 17:44

I have a Java question about generics. I declared a generic list:

List listOfMyType;

Then in some method I try ins

5条回答
  •  没有蜡笔的小新
    2020-11-27 18:38

    You shouldn't need to use the wildcard capture syntax in your case, simply declaring

    List listOfMytype;
    

    should be enough. If you want to know exactly why, the Java Generics Tutorial has more than you would ever want to know about the esoteric craziness of Java Generics. Page 20 addresses your specific case.

    As for why add with the wildcard capture does not work, it is because the compiler can't determine exactly what subclass of MyType the list will be in every case, so the compiler emits an error.

提交回复
热议问题