Java 实现 - 元类

发布于 2025-01-06 12:51:14 字数 184 浏览 3 评论 0原文

按照我的理解,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2025-01-13 12:51:14

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.

半枫 2025-01-13 12:51:14

您的问题针对构造函数没有什么特别的:从元级别描述类的角度来看,构造函数、“普通方法”和字段具有相同的概念。

所以可以这样想:

  • 每个 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:

    • Name of the class
    • the superclass
    • the implemented interfaces
    • a list of constructors and their signatures
    • a list of (static and non-static) methods and their signatures
    • a list of (static and non-static) fields and their types
  • 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 class java.lang.String, another instance of Class describes my.own.class.Foo.

  • java.lang.Class itself is of course also a class - therefore there also exists an instance of Class describing the class Class. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文