gpt4 book ai didi

java - 集合成为原始类型

转载 作者:搜寻专家 更新时间:2023-10-31 08:03:37 26 4
gpt4 key购买 nike

<分区>

例如我们有一些AbsractClass

package inherit;

import java.util.HashSet;
import java.util.Set;

/**
* TODO: Add comment
*
* @author Ruslan Ibragimov
*/
public abstract class AbstractClass<T extends Integer> {

private Set<String> strings = new HashSet<>();

private T value;

public Set<String> getStrings() {
return strings;
}

public void setStrings(Set<String> strings) {
this.strings = strings;
}

public void addString(String string) {
strings.add(string);
}

public T getValue() {
return value;
}

public void setValue(T value) {
this.value = value;
}
}

还有一些 child :

package inherit;

/**
* TODO: Add comment
*
* @author Ruslan Ibragimov
*/
public class InheritClass extends AbstractClass<Integer> {


}

让我们做个简单的测试

package inherit;

/**
* TODO: Add comment
*
* @author Ruslan Ibragimov
*/
public class TestClass {

public static void main(String[] args) {
TestClass testClass = new TestClass();
testClass.test(new InheritClass());
}

private AbstractClass test(AbstractClass aClass) {
for (String string : aClass.getStrings()) {
System.out.println(string);
}
return aClass;
}
}


Make output:

Error:(16, 51) java: incompatible types
required: java.lang.String
found: java.lang.Object
for (String string : aClass.getStrings()) {

为什么会这样?

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