繁体   English   中英

无法将接口的链表实例化为实现该接口的类的链表

[英]Can't instantiate a linkedlist of an interface as a linkedlist of a class that implements the interface

我有:

LinkedList<Interface> fred = new LinkedList<Class>();

其中Interface和Class分别是接口和类的名称,但是java给我一个类型不匹配错误。 我的课程实现了该接口,并成功包含了该接口所要求的所有功能。

为什么这不起作用?

你可以这样做:

LinkedList<MyInterface> fred = new LinkedList<>();

推荐的方法是:

List<MyInterface> fred = new LinkedList<>();

您正在寻找:

LinkedList<Interface> fred = new LinkedList<>();

以下内容无效,因为T的类型在左侧和右侧必须相同,显然不是这种情况。

LinkedList<Interface> fred = new LinkedList<Class>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM