java - Class#isInstance 与 Class#isAssignableFrom

标签 java class instance

<分区>

给定一个 Class<?> superType和一个 Object subInstance , 之间有什么区别

superType.isInstance(subInstance)

superType.isAssignableFrom(subInstance.getClass())

(如果有的话)?

最佳答案

在场景中,没有区别。

两种方法的区别在于参数。一种使用对象,另一种使用类:

Class#isInstance(Object)

Determines if the specified Object is assignment-compatible with the object represented by this Class.

Class#isAssignableFrom(Class)

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.

关于java - Class#isInstance 与 Class#isAssignableFrom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19664009/

相关文章:

c++ - 枚举作为函数参数

java - 是否可以使用 Process Builder 在 Java 中的进程之间共享变量

java - 使用 jar 文件和资源创​​建 exe

java - 导入和扩展

c++ - 首先在 g++ 中从类到命名空间定义

python - 如何访问添加到类的方法中的 "__"(双下划线)变量

java - field.get 和对象实例名称

python - 类实例中未保存的随机值

python - Python 中类的单个实例

java - @Transactional 和继承