簡體   English   中英

為什么在編譯時出現類型不匹配

[英]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>();

您仍然可以將AttrKnotenGewKante對象添加到_graph


另外,如果只需要AttrKontenGewKante對象,則將其聲明為:

private Graph<AttrKnoten, GewKante> _graph;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM