带有类名的 Java instanceof
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13873933/
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
Java instanceof with class name
提问by GMsoF
I am just curious to ask this, maybe it is quite meaningless.
我只是好奇地问这个,也许这很没有意义。
When we are using instanceof in java, like:
当我们在 java 中使用 instanceof 时,例如:
if (a instanceof Parent){ //"Parent" here is a parent class of "a"
}
why we can't use like below:
为什么我们不能像下面这样使用:
if (a instanceof Parent.class){
}
Does the second 'instanceof' make more sense from the view of strict programming? What is the difference between "Parent" and "Parent.class"?
从严格编程的角度来看,第二个“instanceof”是否更有意义?“Parent”和“Parent.class”有什么区别?
回答by Jon Skeet
What is the difference between "Parent" and "Parent.class"?
“Parent”和“Parent.class”有什么区别?
The latter is a class literal- a way of accessing an object of type Class<Parent>
.
后者是类字面量——一种访问类型对象的方式Class<Parent>
。
The former is just the name of a class, which is used in various situations - when calling static methods, constructors, casting etc.
前者只是一个类的名称,它用于各种情况——调用静态方法、构造函数、强制转换等。
Does the second 'instanceof' make more sense from the view of strict programming?
从严格编程的角度来看,第二个“instanceof”是否更有意义?
Well not as the language is defined - instanceof
onlyworks with the name of a type, neveran expression. If you could write
不是按照语言的定义 -instanceof
仅适用于类型的名称,而不是表达式。如果你能写
if (a instanceof Parent.class)
then I'd expectyou do be able to write:
那么我希望你确实能够写:
Class<?> clazz = Parent.class;
if (a instanceof clazz)
... and that's just not the way it works. On the other hand, there isthe Class.isInstance
method which you can call if you want.
......这不是它的工作方式。在另一方面,是在Class.isInstance
这,如果你愿意,你可以调用方法。
What do you mean by "the view of strict programming" in the first place?
首先,“严格编程的观点”是什么意思?
回答by T.J. Crowder
Parent
is a class, so the second example doesn't make more sense that the first. You're asking if the instance is an instance of the class, a instanceof Parent
is a pretty direct expression of that.
Parent
是一个类,所以第二个例子没有第一个更有意义。你问这个实例是否是类的一个实例,这a instanceof Parent
是一个非常直接的表达。
Parent.class
is an instanceof Class
, so even if the second example compiled (it doesn't, the right-hand of instanceof
can't itself be an instance), it wouldn't check what you want it to check. :-)
Parent.class
是一个实例的Class
,所以即使编译第二个例子中(不,的右手instanceof
不能本身是一个实例),它不会检查你想让它检查什么。:-)
回答by Wyzard
Parent
is the name of a type. Parent.class
is essentially a static variable that refers to an object (specifically, an instance of Class
). You want to ask whether a
is an instance of the Parent
type, not whether it's an instance of an object that is itself an instance of some other type (named Class
).
Parent
是类型的名称。 Parent.class
本质上是一个静态变量,它引用一个对象(特别是 的一个实例Class
)。您想询问是否a
是该Parent
类型的实例,而不是它是否是某个对象的实例,而该对象本身是某个其他类型(名为Class
)的实例。
回答by Sumit Singh
When you write Parent.class
then that means you are creating a java.lang.Class
object for your Parent class.
So if (a instanceof Parent.class){
}
this will not work for you.
当您编写时Parent.class
,这意味着您正在java.lang.Class
为 Parent 类创建一个对象。所以if (a instanceof Parent.class){
}
这对你不起作用。
For more details on Class class take a look of following links :
Class
有关 Class 类的更多详细信息,请查看以下链接:
Class
Instances of the class Class represent classes and interfaces in a running Java application. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.
类 Class 的实例表示正在运行的 Java 应用程序中的类和接口。每个数组也属于一个类,该类反映为一个 Class 对象,该对象由具有相同元素类型和维数的所有数组共享。原始 Java 类型(boolean、byte、char、short、int、long、float 和 double)和关键字 void 也表示为 Class 对象。
回答by Richard JP Le Guen
The static Parent.class
member is actually an object. You could assign it to a variable of type Object
or type Class
if you wanted to:
静态Parent.class
成员实际上是一个对象。如果你想,你可以将它分配给一个 typeObject
或 type的变量Class
:
Object o = Parent.class;
Class c = Parent.class;
Parent
on the other hand isn't an object or a variable: it is a Type Name, as per the Java spec.
Parent
另一方面,它不是对象或变量:根据 Java 规范,它是一个Type Name。
If you could do this...
如果你能做到这一点...
a instanceof Parent.class
Since Parent.class
is an objectthen you could feasibly could also do this:
既然Parent.class
是一个对象,那么你也可以这样做:
Cat myCat = new DomesticLonghair();
a instanceof myCat;
... which is just silly.
......这只是愚蠢的。