类成员是从哪里来的?

huangapple 未分类评论44阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年4月5日 14:39:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/61038930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定