本文介绍了java:从List< SomeClass>转换列出< SomeInterface> SomeClass实现SomeInterface时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个脑筋急转弯:我有一个 public interface SomeInterface 和一个静态私有类SomeClass 和我试图从我的一个方法中返回一个 List< SomeInterface> ,但是我得到了错误(在返回列表中; $ c $
类型不匹配:无法从List< GenericList1.SomeClass>到
List< GenericList1.SomeInterface>
如何解决此问题而无需创建新列表?
澄清:我不希望将列表创建为 List< SomeInterface> (可能是显而易见的解决方案),因为私下我想维护一个 List< SomeClass> ,以允许将来访问SomeClass的方法超出公共接口中的方法。这在下面的例子中没有显示,但在我真正的程序中,我需要这个。
pre $ import $ java.util.ArrayList ;
import java.util.List;
public class GenericList1 {
public interface SomeInterface
{
public int getX();
}
私有静态类SomeClass实现SomeInterface
{
final private int x;
@Override public int getX(){
return this.x;
}
public SomeClass(int x){this.x = x; }
}
public static void main(String [] args){
List< SomeInterface> list = createList(10);
printList(list);
$ b $ private static void printList(List< SomeInterface> list){
for(SomeInterface si:list)
System.out.println(si.getX( ));
}
private static List< SomeInterface> createList(int n){
List< SomeClass> list = new ArrayList< SomeClass>();
for(int i = 0; i< n; ++ i)
list.add(new SomeClass(i));
返回列表;
$ div $解析方案你应该重新定义您的方法
private static List
和其他列表声明类似。这允许您以多态方式处理泛型列表,只要您只读取它们的值即可。
(如果您想添加新元素到一个列表中,您应该使用 List< ;? super SomeInterface> 来代替。)
缩写 PECS - Producer:Extends / Consumer:Super ,由Josh Bloch在Effective Java 2nd Edition,第28条中创造。
正如其他人所指出的,这个习语是必要的,因为 List< SomeClass> 不是 a 列表< SomeInterface> ; ,甚至当 SomeClass实现SomeInterface 。原因在所引用的文件中有详细解释。
I'm having a brain cramp: I have a public interface SomeInterface and a static private class SomeClass and am trying to return a List<SomeInterface> from one of my methods, but I get the error (on the return list; line below):
Type mismatch: cannot convert from List<GenericList1.SomeClass> to
List<GenericList1.SomeInterface>
How can I fix this without having to create a new list?
Clarification: I do not want the list to be created as List<SomeInterface> (perhaps the obvious solution) because privately I want to maintain a List<SomeClass> to allow future access to SomeClass's methods beyond the ones in the public interface. This isn't shown in the example case below, but in my real program I need this.
import java.util.ArrayList;
import java.util.List;
public class GenericList1 {
public interface SomeInterface
{
public int getX();
}
private static class SomeClass implements SomeInterface
{
final private int x;
@Override public int getX() {
return this.x;
}
public SomeClass(int x) { this.x = x; }
}
public static void main(String[] args) {
List<SomeInterface> list = createList(10);
printList(list);
}
private static void printList(List<SomeInterface> list) {
for (SomeInterface si : list)
System.out.println(si.getX());
}
private static List<SomeInterface> createList(int n) {
List<SomeClass> list = new ArrayList<SomeClass>();
for (int i = 0; i < n; ++i)
list.add(new SomeClass(i));
return list;
}
}
解决方案
You should redefine your method as
private static List<? extends SomeInterface> createList(int n) { ...
and similarly, the other list declarations. This allows you to deal with generic lists polymorphically, as long as you only read values from them.
(Should you want to add new elements to a list, you should use List<? super SomeInterface> instead.)
This idiom is known by the abbreviation PECS - Producer: Extends / Consumer: Super, coined by Josh Bloch in Effective Java 2nd Edition, Item 28.
As others have noted, this idiom is needed because a List<SomeClass> is not a List<SomeInterface>, even when SomeClass implements SomeInterface. The reason for this is thoroughly explained in the referred document.
这篇关于java:从List< SomeClass>转换列出< SomeInterface> SomeClass实现SomeInterface时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!