简体   繁体   English

Java实现-元类

[英]Java implementation - Meta classes

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?). 以我的理解,Java对象模型是3个级别,每个级别都描述了其下面的级别,因此所有类(本身就是对象?)共享一个Meta类。

My question is - how are constructors implemented in Java? 我的问题是-构造函数如何用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.. (或任何其他类方法)我的逻辑说构造函数应该出现在Meta类中,但是由于只有一个Meta类,因此保留所有可能的构造函数没有任何意义,或者我对这的理解是全部错误..

In Java there's a single metaclass: the instances of the class Class are used to represent the types of classes and interfaces. 在Java中,只有一个元类:类Class的实例用于表示类和接口的类型。 The constructors are defined at the class level, not at the metaclass level. 构造函数是在类级别而不是在元类级别定义的。

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: Java中的每个类都由一组特定的信息描述:

    • 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". 为了您的方便起见,在运行时可以使用此信息-这是“反射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 . 由于可以为JVM加载的每个类提供相同类型的信息,因此将其捆绑在一个名为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 . 因此, Class一个实例描述了类java.lang.StringClass另一个实例描述了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 . java.lang.Class本身当然也是一个类-因此也存在一个Class实例来描述Class And I think that's where things get recursive somehow. 我认为那是事情以某种方式递归的地方。

Summary: There is only one metaclass: java.lang.Class . 简介:只有一个元类: 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. 构造函数描述是元类实例的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM