Java 实现 - 元类
按照我的理解,Java对象模型有3个级别,每个级别描述它下面的级别,因此所有类(它们本身就是对象?)共享一个元类。
我的问题是 - Java 中的构造函数是如何实现的? (或任何其他类方法)我的逻辑是构造函数应该出现在 Meta 类中,但由于只有一个 Meta 类,因此保留所有可能的构造函数没有任何意义,或者我对此的理解是全部错误的..
The way I understand it, Java object model is 3 levels, each level describes the level beneath it, therefore there is one Meta class shared by all Classes (which are themselves objects?).
My question is - how are constructors implemented in Java? (or any other class methods) my logic says that constructors should appear in the Meta classes, but since there is only one Meta class, it doesn't make any sense that it keeps all possible constructors, or is my understanding of this is all wrong..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java 中有一个元类:类
Class
的实例用于表示类和接口的类型。构造函数是在类级别定义的,而不是在元类级别定义的。In Java there's a single metaclass: the instances of the class
Class
are used to represent the types of classes and interfaces. The constructors are defined at the class level, not at the metaclass level.您的问题针对构造函数没有什么特别的:从元级别描述类的角度来看,构造函数、“普通方法”和字段具有相同的概念。
所以可以这样想:
每个 Java 中的类都是由一组特定的信息来描述的:
为了方便起见,您可以在运行时使用此信息 - 这是“反射 API”。
由于 JVM 加载的每个类都可以使用相同类型的信息,因此将其捆绑在名为
java.lang.Class
的自己的类中。因此,类
Class
的一个实例描述了类java.lang.String
,另一个实例描述了Class
描述了my.own.class.Foo
。java.lang.Class
本身当然也是一个类 - 因此还存在一个描述类Class
的实例 >类。我认为这就是事情以某种方式递归的地方。摘要:只有一个元类:
java.lang.Class
。元类的多个实例(元实例?)描述各个类 - 包括元类本身。构造函数描述是元类实例的一部分。Your question targets nothing special about constructors: From the point of describing classes on a metalevel there is the same concept for constructors, "normal methods" and fields.
So think of it this way:
Each class in Java is described by a certain set of informations:
For your convenience this information is available to you during runtime - this is the "reflection API".
Since the same type of information is available for each class loaded by the JVM, this is bundled in a own class named
java.lang.Class
.So one instance of the class
Class
describes the classjava.lang.String
, another instance ofClass
describesmy.own.class.Foo
.java.lang.Class
itself is of course also a class - therefore there also exists an instance ofClass
describing the classClass
. And I think that's where things get recursive somehow.Summary: There is only one metaclass:
java.lang.Class
. Multiple instances (meta-instance?) of the metaclass describe individual classes - including the metaclass itself. Constructor descriptions are part of the instances of the metaclass.