1
今天我偶然發現了這個錯誤,我並不完全明白問題所在。類型與Java泛型和匿名類不匹配
我有一個簡單的界面
public interface MyInterface
{
}
然後我嘗試創建一個Guava ImmutableSet使用of()
方法傳遞一個匿名類的實例。
ImmutableSet<MyInterface> a = ImmutableSet.of(new MyInterface()
{
});
編譯器不喜歡它:
Type mismatch: cannot convert from ImmutableSet<new MyInterface(){}> to ImmutableSet<MyInterface>
什麼樣的類型是ImmutableSet<new MyInterface(){}>
?我不熟悉這個語法。
嘗試'<?實現MyInterface>'。 (不確定)。 :-) – Astrobleme
[Java泛型類型轉換拼圖]可能的重複(http://stackoverflow.com/questions/10555773/java-generics-type-conversion-puzzle) – durron597
@ durron597我不同意。我面臨的問題與匿名類直接相關。 – GuiSim