我之前問過這個問題,但從未得到解決方案。將子類添加到列表中 - 方法add(捕獲#2-of?擴展了TopBean)...不適用於參數(TopBean)
我想添加一個子類型定義如下。這應該是100%合法的,子類型擴展了主類型。
private List<? extends TopBean> beans;
public List<? extends TopBean> getBeans() {
return beans;
}
我試圖做到這一點:
this.getBeans().add(new SecondaryBean());
SecondaryBean延伸TopBean(不直接,但在一定程度上)。
這是推動我堅果 - 錯誤是
The method add(capture#2-of ? extends TopBean) in the type List<capture#2-of ? extends TopBean> is not applicable for the arguments (SecondaryBean)
如果我將其轉換爲TopBean,同樣的錯誤,只是diffent措辭
牛逼he method add(capture#2-of ? extends TopBean) in the type List<capture#2-of ? extends TopBean> is not applicable for the arguments (TopBean)
我必須添加子SecondaryBean進入通用TopBeans列表。在這裏做什麼?