3
類型的返回對象
下面的代碼包含一個getter的設置與T類對象:與泛型類類型
public class Cube <T extends State> {
private Set<T> states = new HashSet<>();
public Set<T> getStates() {
return states;
}
}
這似乎給我回狀態的設置有效的方式。但是,它會返回一組對象。嘗試使用:
Cube<DestinationState> cube = new Cube<>();
Set<DestinationState> set = cube.getStates();
收益率第二行編譯錯誤:
Error:(187, 61) java: incompatible types: java.lang.Object cannot be converted to nl.gijspeters.pubint.graph.state.DestinationState
國家是有多個實現類的通用接口(其中DestinationState)和子。
這可能是一個重複的問題(因爲它看起來很基本),但是,我無法找到答案。
變化'新立方()的類型推斷;'到'新立方<>();在' –
錯字問題,在實際的代碼中就是這種情況。修復。 –
剛剛在我的「Playground」項目中試過了你的代碼,沒有編譯錯誤,儘管 –