下面哪个比较好?
a instanceof B
or
B.class.isAssignableFrom(a.getClass())
我所知道的唯一区别是,当'a'为空时,第一个返回false,而第二个抛出异常。除此之外,它们总是给出相同的结果吗?
下面哪个比较好?
a instanceof B
or
B.class.isAssignableFrom(a.getClass())
我所知道的唯一区别是,当'a'为空时,第一个返回false,而第二个抛出异常。除此之外,它们总是给出相同的结果吗?
当前回答
Instanceof也不能用于基本类型或泛型类型。如下代码所示:
//Define Class< T > type ...
Object e = new Object();
if(e instanceof T) {
// Do something.
}
错误是:不能对类型参数t执行instanceof检查,而是使用它的erasure对象,因为进一步的泛型类型信息将在运行时被擦除。
由于删除运行时引用的类型擦除而无法编译。然而,下面的代码将编译:
if( type.isAssignableFrom(e.getClass())){
// Do something.
}
其他回答
除了上面提到的基本区别之外,在类中的instanceof操作符和isAssignableFrom方法之间还有一个核心的微妙区别。
Read instanceof as “is this (the left part) the instance of this or any subclass of this (the right part)” and read x.getClass().isAssignableFrom(Y.class) as “Can I write X x = new Y()”. In other words, instanceof operator checks if the left object is same or subclass of right class, while isAssignableFrom checks if we can assign object of the parameter class (from) to the reference of the class on which the method is called. Note that both of these consider the actual instance not the reference type.
考虑一个A、B和C三个类的例子,其中C扩展了B, B扩展了A。
B b = new C();
System.out.println(b instanceof A); //is b (which is actually class C object) instance of A, yes. This will return true.
System.out.println(b instanceof B); // is b (which is actually class C object) instance of B, yes. This will return true.
System.out.println(b instanceof C); // is b (which is actually class C object) instance of C, yes. This will return true. If the first statement would be B b = new B(), this would have been false.
System.out.println(b.getClass().isAssignableFrom(A.class));//Can I write C c = new A(), no. So this is false.
System.out.println(b.getClass().isAssignableFrom(B.class)); //Can I write C c = new B(), no. So this is false.
System.out.println(b.getClass().isAssignableFrom(C.class)); //Can I write C c = new C(), Yes. So this is true.
我们在团队中做的一些测试表明,A.class. isassignablefrom (B. getclass())比B instanceof a工作得更快,如果你需要对大量元素进行检查,这是非常有用的。
考虑以下情况。假设你想检查类型A是否是obj类型的超类,你也可以这样做
... A.class.isAssignableFrom(obj.getClass()) ...
OR
... A . obj instanceof ...
但是isAssignableFrom解决方案要求obj的类型在这里是可见的。如果不是这样(例如,obj的类型可能是一个私有内部类),则该选项无效。然而,instanceof solution总是可以工作的。
在使用instanceof时,您需要在编译时知道B的类。当使用isAssignableFrom()时,它可以是动态的,并在运行时更改。
还有一个不同之处。如果要测试的类型(Class)是动态的,例如,作为方法参数传递,那么instanceof将不会为你切割它。
boolean test(Class clazz) {
return (this instanceof clazz); // clazz cannot be resolved to a type.
}
但是你可以:
boolean test(Class clazz) {
return (clazz.isAssignableFrom(this.getClass())); // okidoki
}
哎呀,这个答案已经讲过了。也许这个例子对某人有帮助。