“实例的非法通用类型"在内部类类型上使用instanceof时? [英] "illegal generic type of instanceof" when using instanceof on an inner class type?

查看:196
本文介绍了“实例的非法通用类型"在内部类类型上使用instanceof时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NetBeans中编写了如下代码:

I coded in NetBeans something like this:

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()方法.

The error is in the method equals() from inner class "Par".

NetBeans说该错误是"instanceof的非法通用类型".错误在下面的行中.

NetBeans says that the error is "illegal generic type of instanceof". The error is in the line below.

            if(ob instanceof Par) {

错误原因是什么?

推荐答案

尝试ob instanceof Grafo<?,?>.Par

我认为编译器认为ob instanceof Par涉及对泛型类型参数的运行时检查;即等同于ob instanceof Grafo<V,E>.Par.但是instanceof测试无法检查泛型类型参数.

I think that the compiler thinks that ob instanceof Par involves a runtime check on generic type parameters; i.e. that it is equivalent to ob instanceof Grafo<V,E>.Par. But instanceof tests cannot check generic type parameters.

这篇关于“实例的非法通用类型"在内部类类型上使用instanceof时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆