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 clazzbe some Classand objbe 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 clazzobject is a superclass or superinterface of Foo.
clazz.isAssignableFrom(Foo.class)只要clazz对象表示的类是 的超类或超接口,则为真Foo。
clazz.isInstance(obj)will be true whenever the object objis 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 clazzand objare 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 isInstancebut just the Classobject 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 MyClasswithout raising a ClassCastException. In other words, obj is an instance of MyClassor its subclasses.
MyClass.class.isInstance(obj)用于检查实例。当参数 obj 为非空并且可以在MyClass不引发 a 的情况下强制转换时,它返回 true ClassCastException。换句话说,obj 是一个实例MyClass或其子类。
MyClass.class.isAssignableFrom(Other.class)will return true if MyClassis the same as, or a superclass or superinterface of, Other. Othercan be a class or an interface. It answers true if Othercan 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 Xand Yare the same class, or Xis Y's super class or super interface, return true, otherwise, false.
如果X和Y是同一个类,或者X是Y的超类或超接口,则返回真,否则为假。
X.class.isInstance(y)
X.class.isInstance(y)
Say yis an instance of class Y, if Xand Yare the same class, or Xis Y's super class or super interface, return true, otherwise, false.
说y是一个类的实例Y,如果X和Y是同一个类,或者X是Y的超类或超接口,则返回true,否则返回false。

