有人能告诉我为什么会出现编译错误吗?我不明白为什么在第二个for循环中转换为A会导致strings()返回一般的对象列表.
import java.util.ArrayList;
import java.util.List;
public class E {
public static void main(String[] args) {
for (String s : new D().strings()) {
System.out.println("s = " + s);
}
for (String s : ((A) new D()).strings()) {
System.out.println("s = " + s);
}
}
static class D extends A<C> {
}
static abstract class A<T extends B> {
List<String> strings() {
return new ArrayList<String>() {{
add("Foo");
add("Bar!");
}};
}
}
static class B {
}
static class C extends B { …Run Code Online (Sandbox Code Playgroud)