Can't add value to the Java collection with wildcard generic type

后端 未结 4 1711
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 12:06

Why this code does not compile (Parent is an interface)?

List list = ...
Parent p = factory.get();   // returns concrete         


        
4条回答
  •  礼貌的吻别
    2020-11-22 12:58

    List
    

    PECS - "Producer - Extends, Consumer - Super". Your List is a consumer of Parent objects.

提交回复
热议问题