Java 中是否有类似 instanceOf(Class<?> c) 的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/949352/
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
Is there something like instanceOf(Class<?> c) in Java?
提问by snakile
I want to check if an object o
is an instance of the class C
or of a subclass of C
.
我想检查一个对象o
是类的实例C
还是C
.
For instance, if p
is of class Point
I want x.instanceOf(Point.class)
to be true
and also x.instanceOf(Object.class)
to be true
.
例如,如果p
是类,Point
我想x.instanceOf(Point.class)
成为true
并且也x.instanceOf(Object.class)
成为true
.
I want it to work also for boxed primitive types. For instance, if x
is an Integer
then x.instanceOf(Integer.class)
should be true
.
我希望它也适用于盒装原始类型。例如,如果x
是Integer
然后x.instanceOf(Integer.class)
应该是true
。
Is there such a thing? If not, how can I implement such a method?
有这样的事情吗?如果没有,我该如何实现这样的方法?
采纳答案by gustafc
Class.isInstancedoes what you want.
Class.isInstance做你想要的。
if (Point.class.isInstance(someObj)){
...
}
Of course, you shouldn't use it if you could use instanceof
instead, but for reflection scenarios it often comes in handy.
当然,如果你可以使用它,你不应该使用它instanceof
,但是对于反射场景,它通常会派上用场。
回答by Erich Kitzmueller
x instanceof Integer
x instanceof Object
you just have to use the right syntax
你只需要使用正确的语法
for primitve types, you have to do it completely different. Since you cannot create methods for them , you need a class that keeps the method. So instead of "x.instanceOf(Integer.Class)", you have to call "MyClassComparer.instanceOf(x, Integer.Class)" or something like that. This could easily be implemented by overloading methods, but I fail to see a case when that functionality would be desireable at all.
对于原始类型,您必须完全不同。由于您无法为它们创建方法,因此您需要一个保留该方法的类。所以,而不是“x.instanceOf(Integer.Class)”,你必须调用“MyClassComparer.instanceOf(x, Integer.Class)”或类似的东西。这可以通过重载方法轻松实现,但我没有看到完全需要该功能的情况。
回答by fmsf
You can do:
你可以做:
if (foo instanceof classNameYouWantToCheck)
回答by victor hugo
In fact in Java there's a boolean operator called instanceof which can be used to determine if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
事实上,在 Java 中有一个名为 instanceof 的布尔运算符,它可用于确定对象是类的实例、子类的实例还是实现特定接口的类的实例。
if(obj instanceof SomeClass) {
// Do something
}
The Java Tutorial has a good exampleof this operator
Java 教程有一个很好的例子来说明这个操作符
回答by Michael Borgwardt
I want to check if an object o is an instance of the class c or of a subclass of c. For instance, if p is of class Point I want x.instanceOf(Point.class)
我想检查对象 o 是类 c 的实例还是 c 的子类的实例。例如,如果 p 属于 Point 我想要 x.instanceOf(Point.class)
Um... What? What are o, p and x?
嗯什么?什么是 o、p 和 x?
I want it to work also for primitive types. For instance, if x is an integer then x.instanceOf(Integer.class) and also x.instanceOf(Object.class) should be true.
我希望它也适用于原始类型。例如,如果 x 是一个整数,那么 x.instanceOf(Integer.class) 和 x.instanceOf(Object.class) 应该是真的。
No. It shouldn't even compile. Primitives are not objects, and you cannot call methods on them.
不,它甚至不应该编译。原语不是对象,您不能对它们调用方法。
Anyway, there are three things, one of which can definitely achieve what you want (they differ somewhat in where exactly the apply:
无论如何,有三件事,其中之一绝对可以实现您想要的(它们在应用的确切位置上有所不同:
- The
instanceof
operator if you know the class at compile time. - Class.isInstance()if you want to check an object's class against a class not known at compile time.
- Class.isAssignableFrom()if you want to check the assignability given two class objects.
- 该
instanceof
运营商如果你知道在编译时的类。 - Class.isInstance()如果您想根据编译时未知的类检查对象的类。
- Class.isAssignableFrom()如果要检查给定两个类对象的可分配性。
回答by newacct
"I want it to work also for primitive types. For instance, if x is an integer then x.instanceOf(Integer.class) and also x.instanceOf(Object.class) should be true."
“我希望它也适用于原始类型。例如,如果 x 是一个整数,那么 x.instanceOf(Integer.class) 和 x.instanceOf(Object.class) 应该是真的。”
Why? Primitive types and reference types are completely separate. A primitive int is not a subtype of Integer nor Object. The type of a primitive value is always known statically at compile time, so there is no point in testing its type. Sure, you can box a primitive value and then test its type:
为什么?原始类型和引用类型是完全分开的。原始 int 不是 Integer 或 Object 的子类型。原始值的类型在编译时始终是静态已知的,因此测试其类型没有意义。当然,你可以装箱一个原始值,然后测试它的类型:
(Integer)5 instanceof Integer
But what would be the point?
但重点是什么?
回答by Paul Bors
I think you're confused about instanceof for raw objects and generic ones
我认为您对原始对象和通用对象的 instanceof 感到困惑
obj instanceof Class
obj instanceof Class<?> // this is the one you want