![](/img/trans.png)
[英]What is the VB equivalent of Java's instanceof and isInstance()?
[英]What does it mean isInstance is a 'dynamic equivalent' of instanceof?
什么意思'动态等价'?
我只是想知道具有this.getClass().isInstance(aClass)
而不是this instanceof aClass
什么? 有区别吗?
确定指定的Object是否与此Class表示的对象分配兼容。 此方法是Java语言instanceof运算符的动态等效项
是。 不仅顺序不一样,而且object instanceof 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()
。
instanceof
运算符测试以查看对象是否是固定(静态)类的实例; 即一个名称在编译时已知的类。
Class.isInstance
方法允许您针对动态类进行测试; 即一个仅在运行时已知的类。
我只是想知道具有
this.getClass().isInstance(aClass)
而不是this instanceof aClass
什么? 有区别吗?
isInstance
的目的如上。
这两个表达式之间的主要区别是:
在第一个中, aClass
是一个变量,其值是一个Class
对象,和
在第二个中, aClass
是类的名称 :它不能是变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.