鉴于这种简单的类:泛型编译问题:不兼容的类型
import java.util.Collection;
public class GenericTest<T> {
public Collection<String> getKeys() {
return null;
}
public void copy(GenericTest a_from) {
for (String x : a_from.getKeys()) {
}
}
}
我收到以下编译错误,但不明白为什么。
error: incompatible types
for (String x : a_from.getKeys()) {
required: String
found: Object
错误消失,如果我改变参数的copy()方法来GenericTest <牛逼>,但是这不是我想要的。 copy()方法在任何类型的GenericTest上都有效,不仅仅是GenericTest <T>。
'a_from'声明在哪里? – christopher
a_from在copy()方法的签名中声明 – Mitch