[英]Why do I get a type mismatch during compile time
我正在聲明一個圖(使用JUNG的Graph接口)作為這樣的類變量:
private Graph<Knoten, Kante> _graph;
我嘗試像這樣初始化它:
_graph = new DirectedSparseGraph<AttrKnoten, GewKante>();
AttrKnoten擴展了Knoten,而GewKante擴展了Kante(目前它們只是標記界面)。 我在編譯時收到以下錯誤消息:
"Type mismatch: cannot convert from DirectedSpareGraph<AttrKnoten, GewKante> to Graph<Knoten, Kante>"
這是為什么? 還有其他方法可以解決此問題,但在聲明過程中會遺漏參數嗎?
使用泛型無法做到這一點。
一個簡單的例子:
List<CharSequence> list = new ArrayList<String>();
即使String
實現CharSequence
,這也不起作用。
最簡單的解決方案是執行以下操作:
_graph = new DirectedSparseGraph<Knoten, Kante>();
您仍然可以將AttrKnoten
和GewKante
對象添加到_graph
。
另外,如果只需要AttrKonten
和GewKante
對象,則將其聲明為:
private Graph<AttrKnoten, GewKante> _graph;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.