这两个示例不使用 Oracle Java 8 JDK 进行编译。
错误是:
error: incompatible types: Object cannot be converted to Integer for (Integer i : foo.set )
示例1)
import java.util.Set;
class Foo<T>
{
protected Set<Integer> set;
}
class Foo2 extends Foo
{
void doit()
{
for (Integer i : set )
{
}
}
}
示例 2)
import java.util.Set;
class Foo<T>
{
public Set<Integer> set;
public static void main( String[] args )
{
Foo foo = new Foo();
for (Integer i : foo.set )
{
}
}
}
这是错误还是功能?据我所知,泛型不适用于原始类型的领域。
最佳答案
这不是一个错误。您需要传递泛型类型来创建新实例:
class Foo<T>
{
public Set<Integer> set;
public static void main( String[] args ) {
Foo<Integer> foo = new Foo(); // changed code
for (Integer i : foo.set )
{
}
}
}
关于java - java8中的有界类型编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42972858/