当使用java泛型时,instanceof奇怪地不编译

时间:2014-10-23 09:13:12

标签: java generics compiler-errors instanceof

有人可以向我解释为什么第一个评估实例不能编译,但第二个实例编译并运行完美吗?

private <C extends ClassA, E extends ClassB & InterfaceB> C 
    doSomething(Class<C> clazz, E entity)
{
    if(entity instanceof SomeEntity){ // This line doesn't compile.
        System.out.println();
    }

    Object tmp = entity;
    if(tmp instanceof SomeEntity){  // This line compiles and runs perfectly.
        System.out.println();
    }
}

0 个答案:

没有答案