gpt4 book ai didi

java - 泛型编译问题 : incompatible types

转载 作者:行者123 更新时间:2023-11-29 03:31:01 28 4
gpt4 key购买 nike

<分区>

给定这个简单的类:

    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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com