什么意思’动态等价’?
我只是想知道具有this.getClass().isinstance(aClass)而不是这个aClass实例的目的是什么?有区别吗?
Determines if the specified Object is assignment-compatible with the
object represented by this Class. This method is the dynamic
equivalent of the Java language instanceof operator
解决方法
是.不仅顺序不一样,而且Clazz的对象实例必须有一个在编译时已知的类. clazz.isinstance(object)可以使用运行时已知的类.
还有一个微妙的区别是isinstance会自动装箱,但是instanceof不会.
例如
10 instanceof Integer // does not compile Integer.class.isinstance(10) // returns true Integer i = 10; if (i instanceof String) // does NOT compile if (String.class.isinstance(i)) // is false
为了看到差异,我建议你尝试使用它们.
注意:如果你执行object.getClass().getClass()或myClass.getClass(),你将获得一个Class注意不要在不需要时调用getClass().
原文地址:https://www.jb51.cc/java/127746.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。