简体   繁体   English

什么是Java中的“运行时类”?

[英]What is a “runtime class” in Java?

I try to understand what the Object.getClass() method does. 我试着理解Object.getClass()方法的作用。

The documentation says that it "returns the runtime class of an object." 文档说它“返回对象的运行时类”。 That explanation doesn't help me understanding the term. 这个解释并没有帮助我理解这个术语。

Has someone a simple description of what a "runtime class" is and what getClass() does? 有人简单描述了“运行时类”是什么以及getClass()作用是什么?

It means "the class of the instance the variable refers to at runtime" (sorry if that's not actually clearer). 它表示“变量在运行时引用的实例的类”(对不起,如果实际上并不清楚)。

If you have a reference to an Object , it could refer to an Object , a String , an Integer ... you get that class, not Object . 如果你有一个对Object的引用,它可以引用一个Object ,一个String ,一个Integer ......你得到的是该类,而不是Object

Object obj1 = new Object();
System.out.println(obj1.getClass());  // java.lang.Object

String obj2 = "";
System.out.println(obj2.getClass());  // java.lang.String

obj1 = obj2;
System.out.println(obj1.getClass());  // java.lang.String, not Object.

Every class you write has a lot of metadata. 您编写的每个类都有很多元数据。 That metadata consists of the class name, its fields, its methods, its base classes, the interfaces it implements, and so on. 该元数据由类名,其字段,方法,基类,它实现的接口等组成。

Sometimes you may need to access that metadata from your code at runtime. 有时您可能需要在运行时从代码中访问该元数据。

In order to do so, you can take any object and call its getClass() method. 为此,您可以获取任何对象并调用其getClass()方法。 You will receive a Class object that will contain the above metadata. 您将收到一个包含上述元数据的Class对象。

Just understand it as "an object that has all the metadata of the object's type". 只需将其理解为“具有对象类型的所有元数据的对象”。 In that object, you can find the methods declared in the class, the fields, the type hierarchy, etc. This information will be typically used by code that uses reflection to either inspect objects/types or to run method without the need to have the class defined and compiled when they, themselves are being coded. 在该对象中,您可以找到在类,字段,类型层次结构等中声明的方法。此信息通常由使用反射来检查对象/类型或运行方法的代码使用,而无需具有当它们本身被编码时定义和编译的类。

"Runtime" may be emphasized because the class definition may change over time, or the object may be declared as a supertype while it actually is an instance of a subtype of the one declared. 可以强调“运行时”,因为类定义可能随时间而变化,或者对象可能被声明为超类型,而实际上它是所声明的子类型的子类型的实例。 When a certain class is loaded, it's that information, as loaded during that instance, that will be returned by the getClass() method. 当加载某个类时,它就是在该实例中加载的信息,它将由getClass()方法返回。

In short, when your code runs, the VM will have a definition of your class in a different way than the "source" form that you type in a .java file. 简而言之,当您的代码运行时,VM将以与您在.java文件中键入的“源”表单不同的方式定义您的类。 That information, of course after being compiled, will be loaded and all the metadata (as said above) will constitute what they call the "runtime class". 当然,在编译之后,该信息将被加载,并且所有元数据(如上所述)将构成他们称之为“运行时类”的信息。 It's just a fancy way to say "an object with all the metadata about a class loaded when the program is running" 这只是一种奇特的方式来说“一个对象,其中包含有关在程序运行时加载的类的所有元数据”

The class of an object can change at runtime. 对象的类可以在运行时更改。 Consider following example: 考虑以下示例:

package demo;

public class Main {

    public static class A {
        public int a=0;
    }

    public static class B extends A {
        public int b=1;
    }

    public static void main(String[] args) {

        Main.A b=new Main.A();

        System.out.println(b.getClass().toString());

        b=new Main.B();

        System.out.println(b.getClass().toString());
    }   

}

The output of b.getClass() changed at runtime. b.getClass()的输出在运行时更改。

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

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