英文:
Where does the class member come from?
问题
每个Java类X都可以通过X.class获取其类名。
当我在JDK文档中查看时,Object中没有名为class的成员。
class成员是从哪里来的?
英文:
Every Java class X can get its class name from X.class.
When I check it in the JDK documents, there isn't a member named class in Object.
Where does the class member come from?
答案1
得分: 0
Foo.class 不是 Foo 的成员,尽管语法看起来是这样的。
实际上,Foo.class 是一个类字面表达式。
这意味着它只是语言中用来引用给定类型的类的特殊语法。
如果你使用反射来检查 Foo,你不会看到一个静态字段 class。
英文:
Foo.class is not a member of Foo, even though the syntax looks like it.
Foo.class is actually a Class Literal expression.
That means that it's just a special syntax in the language that's used to refer to the class of a given type.
If you used reflection to inspect Foo you wouldn't see a static field class.
专注分享java语言的经验与见解,让所有开发者获益!

评论