无法将接口的链表实例化为实现该接口的 class 的链表
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 是接口的名称,class 是恭敬的,但是 java 给我一个类型不匹配错误。我的class实现了接口,成功包含了接口调用的所有函数
为什么这不起作用?
你可以这样做:
LinkedList<MyInterface> fred = new LinkedList<>();
推荐的方法是:
List<MyInterface> fred = new LinkedList<>();
您正在寻找:
LinkedList<Interface> fred = new LinkedList<>();
以下内容无效,因为 T
的类型必须在左侧和右侧都相同,很明显,情况并非如此。
LinkedList<Interface> fred = new LinkedList<Class>();
我有:
LinkedList<Interface> fred = new LinkedList<Class>();
其中 Interface 和 Class 是接口的名称,class 是恭敬的,但是 java 给我一个类型不匹配错误。我的class实现了接口,成功包含了接口调用的所有函数
为什么这不起作用?
你可以这样做:
LinkedList<MyInterface> fred = new LinkedList<>();
推荐的方法是:
List<MyInterface> fred = new LinkedList<>();
您正在寻找:
LinkedList<Interface> fred = new LinkedList<>();
以下内容无效,因为 T
的类型必须在左侧和右侧都相同,很明显,情况并非如此。
LinkedList<Interface> fred = new LinkedList<Class>();