java:Class.isInstance 与 Class.isAssignableFrom

问题描述:

clazz 成为一些 Classobj 成为一些 Object.

Let clazz be some Class and obj be some Object.

clazz.isAssignableFrom(obj.getClass())

总是一样

clazz.isInstance(obj)

?

如果不是,有什么区别?

If not, what are the differences?

clazz.isAssignableFrom(Foo.class) 每当 clazz 对象表示的类时,都会为真是 Foo 的超类或超接口.

clazz.isAssignableFrom(Foo.class) will be true whenever the class represented by the clazz object is a superclass or superinterface of Foo.

clazz.isInstance(obj) 就会为真.

即:

clazz.isAssignableFrom(obj.getClass()) == clazz.isInstance(obj)

只要 clazzobj 不为空,

总是为真.

is always true so long as clazz and obj are nonnull.