检查对象是否属于 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 instanceofkeyword, as described by the other answers, is usually what you would want.
Keep in mind that instanceofwill return truefor 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 ais an instance of X, but not if ais an instance of a subclass of X.
在上面的例子中,如果a是 的实例,则条件为真X,但如果a是 的子类的实例,则条件不成立X。
In comparison:
相比下:
if (a instanceof X) {
// do something
}
In the instanceofexample, the condition is true if ais an instance of X, or if ais an instance of a subclassof X.
在instanceof示例中,如果a是 的实例X,或者如果a是 的子类的实例,则条件为真X。
Most of the time, instanceofis right.
大多数时候,instanceof是对的。
回答by Kos
Try operator instanceof.
试试运营商instanceof。
回答by Jacob Relkin
回答by gdejohn
回答by Justin Muller
I agree with the use of instanceofalready mentioned.
我同意使用instanceof已经提到的。
An additional benefit of using instanceofis that when used with a nullreference instanceofof 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 Ain 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 castwarning):
并且以下将不起作用(并且会产生unchecked cast警告):
<A> void someMethod(Object a) {
try {
A casted = (A)a;
} catch (ClassCastException e) {
...
}
}
You can't cast to Aat runtime, because at runtime, Ais essentially Object.
您不能A在运行时强制转换为,因为在运行时,A本质上是Object.
The solutions to such cases is to use a Classinstead 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);

