给定这个简单的类:
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<T>,错误就会消失,但这不是我想要的。copy() 方法对任何类型的 GenericTest 都有效,而不仅仅是 GenericTest<T>。