【发布时间】:2009-03-19 14:53:23
【问题描述】:
请考虑以下 sn-p:
public interface MyInterface {
public int getId();
}
public class MyPojo implements MyInterface {
private int id;
public MyPojo(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
public ArrayList<MyInterface> getMyInterfaces() {
ArrayList<MyPojo> myPojos = new ArrayList<MyPojo>(0);
myPojos.add(new MyPojo(0));
myPojos.add(new MyPojo(1));
return (ArrayList<MyInterface>) myPojos;
}
return 语句进行了无法编译的强制转换。如何将 myPojos 列表转换为更通用的列表,无需遍历列表中的每个项目?
谢谢
【问题讨论】: