检查对象是否属于 Java 中的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4294844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if an object belongs to a class in Java
提问by chimeracoder
Is there an easy way to verify that an object belongs to a given class? For example, I could do
有没有一种简单的方法来验证一个对象是否属于给定的类?例如,我可以做
if(a.getClass() = (new MyClass()).getClass())
{
//do something
}
but this requires instantiating a new object on the fly each time, only to discard it. Is there a better way to check that "a" belongs to the class "MyClass"?
但这需要每次都动态实例化一个新对象,然后丢弃它。有没有更好的方法来检查“a”是否属于“MyClass”类?
采纳答案by dhm
The instanceof
keyword, as described by the other answers, is usually what you would want.
Keep in mind that instanceof
will return true
for superclasses as well.
如instanceof
其他答案所述,关键字通常是您想要的。请记住,超类instanceof
也会返回true
。
If you want to see if an object is a direct instance of a class, you could compare the class. You can get the class object of an instance via getClass()
. And you can statically access a specific class via ClassName.class
.
如果你想查看一个对象是否是一个类的直接实例,你可以比较这个类。您可以通过 获取实例的类对象getClass()
。您可以通过静态访问特定类ClassName.class
。
So for example:
例如:
if (a.getClass() == X.class) {
// do something
}
In the above example, the condition is true if a
is an instance of X
, but not if a
is an instance of a subclass of X
.
在上面的例子中,如果a
是 的实例,则条件为真X
,但如果a
是 的子类的实例,则条件不成立X
。
In comparison:
相比下:
if (a instanceof X) {
// do something
}
In the instanceof
example, the condition is true if a
is an instance of X
, or if a
is an instance of a subclassof X
.
在instanceof
示例中,如果a
是 的实例X
,或者如果a
是 的子类的实例,则条件为真X
。
Most of the time, instanceof
is right.
大多数时候,instanceof
是对的。
回答by Kos
Try operator instanceof
.
试试运营商instanceof
。
回答by Jacob Relkin
回答by gdejohn
回答by Justin Muller
I agree with the use of instanceof
already mentioned.
我同意使用instanceof
已经提到的。
An additional benefit of using instanceof
is that when used with a null
reference instanceof
of will return false
, while a.getClass()
would throw a NullPointerException
.
using 的另一个好处instanceof
是,当与of的null
引用一起使用时instanceof
将返回false
,而a.getClass()
将抛出一个NullPointerException
。
回答by Martin Konicek
The usual way would be:
通常的方法是:
if (a instanceof A)
However, there are cases when you can't do this, such as when A
in a generic argument.
但是,有些情况下您不能这样做,例如A
在泛型参数中时。
Due to Java's type erasure, the following won't compile:
由于 Java 的类型擦除,以下将无法编译:
<A> boolean someMethod(Object a) {
if (a instanceof A)
...
}
and the following won't work (and will produce an unchecked cast
warning):
并且以下将不起作用(并且会产生unchecked cast
警告):
<A> void someMethod(Object a) {
try {
A casted = (A)a;
} catch (ClassCastException e) {
...
}
}
You can't cast to A
at runtime, because at runtime, A
is essentially Object
.
您不能A
在运行时强制转换为,因为在运行时,A
本质上是Object
.
The solutions to such cases is to use a Class
instead of the generic argument:
这种情况的解决方案是使用 aClass
而不是泛型参数:
void someMethod(Object a, Class<A> aClass) {
if (aClass.isInstance(a)) {
A casted = aClass.cast(a);
...
}
}
You can then call the method as:
然后,您可以调用该方法:
someMethod(myInstance, MyClass.class);
someMethod(myInstance, OtherClass.class);