java:Class.isInstance 与 Class.isAssignableFrom
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3949260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java: Class.isInstance vs Class.isAssignableFrom
提问by Albert
Let clazz
be some Class
and obj
be some Object
.
让clazz
成为一些Class
,obj
成为一些Object
。
Is
是
clazz.isAssignableFrom(obj.getClass())
always the same as
总是一样
clazz.isInstance(obj)
?
?
If not, what are the differences?
如果不是,有什么区别?
采纳答案by uckelman
clazz.isAssignableFrom(Foo.class)
will be true whenever the class represented by the clazz
object is a superclass or superinterface of Foo
.
clazz.isAssignableFrom(Foo.class)
只要clazz
对象表示的类是 的超类或超接口,则为真Foo
。
clazz.isInstance(obj)
will be true whenever the object obj
is an instance of the class clazz
.
clazz.isInstance(obj)
只要对象obj
是类的实例,就会为真clazz
。
That is:
那是:
clazz.isAssignableFrom(obj.getClass()) == clazz.isInstance(obj)
is always true so long as clazz
and obj
are nonnull.
只要clazz
和 不obj
为空,就始终为真。
回答by ColinD
I think the result for those two should always be the same. The difference is that you need an instance of the class to use isInstance
but just the Class
object to use isAssignableFrom
.
我认为这两者的结果应该始终相同。不同之处在于您需要使用类的实例,isInstance
而只Class
需要使用对象isAssignableFrom
。
回答by Paul
Both answers are in the ballpark but neither is a complete answer.
两个答案都在大致范围内,但都不是完整的答案。
MyClass.class.isInstance(obj)
is for checking an instance. It returns true when the parameter obj is non-null and can be cast to MyClass
without raising a ClassCastException
. In other words, obj is an instance of MyClass
or its subclasses.
MyClass.class.isInstance(obj)
用于检查实例。当参数 obj 为非空并且可以在MyClass
不引发 a 的情况下强制转换时,它返回 true ClassCastException
。换句话说,obj 是一个实例MyClass
或其子类。
MyClass.class.isAssignableFrom(Other.class)
will return true if MyClass
is the same as, or a superclass or superinterface of, Other
. Other
can be a class or an interface. It answers true if Other
can be converted to a MyClass
.
MyClass.class.isAssignableFrom(Other.class)
如果MyClass
与 相同,或者是 的超类或超接口,则返回 true Other
。 Other
可以是类或接口。如果Other
可以转换为MyClass
.
A little code to demonstrate:
一个小代码来演示:
public class NewMain
{
public static void main(String[] args)
{
NewMain nm = new NewMain();
nm.doit();
}
class A { }
class B extends A { }
public void doit()
{
A myA = new A();
B myB = new B();
A[] aArr = new A[0];
B[] bArr = new B[0];
System.out.println("b instanceof a: " + (myB instanceof A)); // true
System.out.println("b isInstance a: " + A.class.isInstance(myB)); //true
System.out.println("a isInstance b: " + B.class.isInstance(myA)); //false
System.out.println("b isAssignableFrom a: " + A.class.isAssignableFrom(B.class)); //true
System.out.println("a isAssignableFrom b: " + B.class.isAssignableFrom(A.class)); //false
System.out.println("bArr isInstance A: " + A.class.isInstance(bArr)); //false
System.out.println("bArr isInstance aArr: " + aArr.getClass().isInstance(bArr)); //true
System.out.println("bArr isAssignableFrom aArr: " + aArr.getClass().isAssignableFrom(bArr.getClass())); //true
}
}
回答by Sunny
For brevity, we can understand these two APIs like below:
为简洁起见,我们可以理解这两个 API,如下所示:
X.class.isAssignableFrom(Y.class)
X.class.isAssignableFrom(Y.class)
If X
and Y
are the same class, or X
is Y
's super class or super interface, return true, otherwise, false.
如果X
和Y
是同一个类,或者X
是Y
的超类或超接口,则返回真,否则为假。
X.class.isInstance(y)
X.class.isInstance(y)
Say y
is an instance of class Y
, if X
and Y
are the same class, or X
is Y
's super class or super interface, return true, otherwise, false.
说y
是一个类的实例Y
,如果X
和Y
是同一个类,或者X
是Y
的超类或超接口,则返回true,否则返回false。