[英]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.