繁体   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