我在 NetBeans 中编码如下:
public class Grafo<V, E>
{
class Par
{
int a, b;
Par(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object ob)
{
if(ob instanceof Par) {
Par p = (Par)ob;
return this.a==p.a && this.b==p.b;
}
return false;
}
}
//stuff...
} //end of class Grafo
错误出在内部类“Par”的 equals() 方法中。
NetBeans 说错误是“instanceof 的非法通用类型”。错误在下面的行中。
if(ob instanceof Par) {
错误的原因是什么?
最佳答案
尝试 ob instanceof Grafo<?,?>.Par
我认为编译器认为 ob instanceof Par
涉及对泛型类型参数的运行时检查;即它相当于 ob instanceof Grafo<V,E>.Par
.但是instanceof
测试无法检查泛型类型参数。
关于java - 在内部类类型上使用 instanceof 时出现 "illegal generic type of instanceof"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1895682/