为什么不能“阶级”?变量传递给instanceof?
为什么这段代码不能编译?
public boolean isOf(Class clazz, Object obj){
if(obj instanceof clazz){
return true;
}else{
return false;
}
}
为什么我无法将类变量传递给 instanceof
?
Why doesn't this code compile?
public boolean isOf(Class clazz, Object obj){
if(obj instanceof clazz){
return true;
}else{
return false;
}
}
Why I can't pass a class variable to instanceof
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
instanceof
运算符适用于引用类型,例如Integer
,而不适用于对象,例如new Integer(213)
。您可能想要类似附注:如果您编写“
不确定是否还需要方法”,您的代码将会更加简洁。
The
instanceof
operator works on reference types, likeInteger
, and not on objects, likenew Integer(213)
. You probably want something likeSide note: your code will be more concise if you write
Not really sure if you need a method anymore ,though.
instanceof
只能与显式类名(在编译时声明)一起使用。为了进行运行时检查,您应该这样做:这比 clazz.isAssignableFrom(..) 有一点优势,因为它处理的是
obj = 的情况= null 更好。
instanceof
can be used only with explicit class names (stated at compile time). In order to do a runtime check, you should do:This has a small advantage over
clazz.isAssignableFrom(..)
since it deals with the caseobj == null
better.正如其他人提到的,您不能将类变量传递给
instanceof
,因为类变量引用 Object 的实例,而instanceof
的右手> 必须是类型。也就是说,instanceof并不意味着“y是对象x的实例”,它意味着“y是类型X的实例”。如果您不知道对象和类型之间的区别,请考虑:Object o = new Object();
这里,类型是
Object
,并且o
是对该类型的对象实例的引用。因此:if(o instanceof Object)
有效,但
if(o instanceof o)
无效,因为右侧的
o
是一个对象,不是一个类型。更具体地说,类实例不是类型,它是一个对象(由 JVM 为您创建)。在您的方法中,
Class
是一种类型,但clazz
是一个对象(嗯,对对象的引用)您需要的是一种将对象与类进行比较的方法目的。事实证明,这很流行,因此它作为类对象的方法提供给您:
isInstance()
。这是 isInstance 的 Java 文档,它更好地解释了这一点:
As others have mentioned, you cannot pass a class variable to
instanceof
because a class variable references an instance of an Object, while the right hand ofinstanceof
has to be a type. That is,instanceof
does not mean "y is an instance of Object x", it means "y is an instance of type X". In case you don't know the difference between an Object and a type, consider:Object o = new Object();
Here, the type is
Object
, ando
is a reference to the instance of the Object with that type. Thus:if(o instanceof Object)
is valid but
if(o instanceof o)
is not because
o
on the right hand side is an Object, not a type.More specific to your case, a class instance is not a type, it is an Object (which is created for you by the JVM). In your method,
Class
is a type, butclazz
is an Object (well, a reference to an Object)What you need is an way to compare an Object to a Class Object. It turns out that this is popular so this is provided to you as a method of the Class Object:
isInstance()
.Here is the Java Doc for isInstance, which explains this better:
首先,
instanceof
要求右侧的操作数是一个实际的类(例如obj instanceof Object
或obj instanceof Integer
),而不是变量输入类
。其次,您犯了一个相当常见的新手错误,您确实不应该这样做...以下模式:上面的内容可以重构为:
您应该始终执行该重构,因为它消除了多余的 if...else 语句。同样,表达式
return conditional_expression ? true : false;
可重构为相同的结果。Firstly,
instanceof
requires that the operand on the right is an actual class (e.g.obj instanceof Object
orobj instanceof Integer
) and not a variable of typeClass
. Secondly, you have made a fairly common newbie mistake that you really should not do... the following pattern:The above can be refactored into:
You should always perform that refactoring, as it eliminates a redundant if...else statement. Similarly, the expression
return conditional_expression ? true : false;
is refactorable to the same result.