[英]cast from List<MyClass> to List<Interface>
I think I made some mistake while writing my code. 我认为编写代码时犯了一些错误。 I have a class MyClass that implements Interface, now I've a method that is generic for List of Interface as a parameter. 我有一个实现接口的MyClass类,现在有一个方法作为接口列表的通用参数。 I want to use my method by passing a List. 我想通过一个列表来使用我的方法。 I can't cast from List to List (that I assumed I could). 我无法从列表投射到列表(我以为可以)。 So, what can I do to make my code work? 那么,我该怎么做才能使代码正常工作? here an example: 这里有个例子:
List<MyClass> lista = returnMyClassList();
myMethod((List<Interface>) lista); //this doesn't work
//myMethod signature
public void myMethod(List<Interface> struttura);
Thanks for helping. 感谢您的帮助。
Use an upper bound of Interface
for the type: <? extends Interface>
对类型使用Interface
的上限: <? extends Interface>
<? extends Interface>
. <? extends Interface>
。
Here's come compilable code that uses classes from the JDK to illustrate: 这是使用JDK中的类说明的可编译代码:
public static void myMethod(List<? extends Comparable> struttura) {}
public static void main(String[] args) {
List<Integer> lista = null;
myMethod(lista); // compiles OK
}
You should use public void myMethod(List<? extends Interface> struttura);
您应该使用public void myMethod(List<? extends Interface> struttura);
. 。 This way you will be able to pass any type that IS-A
Interface
. 这样,您将可以传递IS-A
Interface
任何类型。 You would like to read about Polymorpshism and generics 您想阅读有关多态性和泛型的信息
You can use: 您可以使用:
public void myMethod(List<? extends Interface> struttura);
In order to get both List<Interface>
and List<MyClass>
为了同时获得List<Interface>
和List<MyClass>
List<MyClass>
is not a sub type of List<Interface>
you are getting error. List<MyClass>
不是List<Interface>
的子类型,您遇到错误。
Accorind to java docs Accorind到Java 文档
Given two concrete types A and B (for example, Number and Integer), MyClass has no relationship to MyClass, regardless of whether or not A and B are related. 给定两个具体的类型A和B(例如,Number和Integer),无论A和B是否相关,MyClass与MyClass没有关系。 The common parent of MyClass and MyClass is Object MyClass和MyClass的共同父对象是Object
So Change your method signature to 因此,将您的方法签名更改为
public void myMethod(List<? extends Interface> struttura){
}
and call the method 并调用方法
List<MyClass> lista = returnMyClassList();
myMethod((lista);
You cannot cast this, because you would make a promise you cannot hold. 您不能投这个票,因为您会许诺您无法兑现。
When you cast from List<MyClass>
to List<Interface>
you take a list containing several objects of type MyClass and vast it to a type that makes (among others) the following promises: 当从List<MyClass>
为List<Interface>
您将获得一个包含几个MyClass类型的对象的列表,并将其扩大为一个(除其他以外)具有以下承诺的类型:
Interface get(int index)
- you can get an element of thpe Interface (no problem) Interface get(int index)
-您可以获取thpe接口的元素(没问题) boolean add(Interface e)
- you can add any element of type Interface (if it is a subtype of MyClass or not) (this is a problem!) boolean add(Interface e)
-您可以添加Interface类型的任何元素(如果它不是MyClass的子类型)(这是一个问题!) The second point is problematic, because the underlying List is still a list of MyClass
and not a list of Interface
in general. 第二点是有问题的,因为底层的List通常仍然是MyClass
的列表,而不是Interface
的列表。 You could still hold a link to the underlying List in a separate variable and could run get
on that list. 您仍然可以在单独的变量中保留指向基础List的链接,并可以在该列表上运行get
。 If you added an object of type Interface
, that is not also a MyClass
, you could get this Non- MyClass
from the List<MyClass>
. 如果添加了Interface
类型的对象,该对象也不是MyClass
,则可以从List<MyClass>
获得此Non- MyClass
。
As all the other answers and comments have pointed out, the correct solution is the type List<? extends Interface>
正如所有其他答案和评论所指出的那样,正确的解决方案是List<? extends Interface>
类型List<? extends Interface>
List<? extends Interface>
. List<? extends Interface>
。 With this type, you can still get objects of type Interface
from the list (as a return value of a method call), but you cannot put anything into the list (use as a parameter of a method call). 使用这种类型,您仍然可以从列表中获取Interface
类型的对象(作为方法调用的返回值),但是不能将任何东西放入列表中(用作方法调用的参数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.