有两种方法可以获取类的 Class
对象。
静态:
Class cls = Object.class;
来自实例:
Object ob = new Object(); Class cls = ob.getClass();
现在我的问题是 getClass()
是 Object
类中的一个方法,
但是什么是 .class
?它是一个变量吗?如果是,那么它在 Java 中定义在哪里?
最佳答案
它在内部实现并称为由 JVM 处理的类文字。
Java Language Specification专门为它提到了“token”这个词。
所以 .class
不仅仅是一个变量,坦率地说它根本不是一个变量。在更广泛的层面上,您可以将其视为关键字或 token 。
https://docs.oracle.com/javase/specs/jls/se9/html/jls-15.html#jls-15.8.2
A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type
void
, followed by a '.
' and the tokenclass
.A class literal evaluates to the
Class
object for the named type (or forvoid
) as defined by the defining class loader (§12.2) of the class of the current instance.
关于java - .class 在 Java 中定义在哪里? (它是一个变量还是什么?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48375901/