什么是“运行时类"?在Java中? [英] What is a "runtime class" in Java?

查看:1066
本文介绍了什么是“运行时类"?在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解Object.getClass()方法的作用.

I try to understand what the Object.getClass() method does.

文档说,它返回对象的运行时类. "这种解释并不能帮助我理解该术语.

The documentation says that it "returns the runtime class of an object." That explanation doesn't help me understanding the term.

有人对运行时类"是什么以及getClass()的作用做了简单的描述吗?

Has someone a simple description of what a "runtime class" is and what getClass() does?

推荐答案

只需将其理解为具有该对象类型的所有元数据的对象".在该对象中,您可以找到在类,字段,类型层次结构等中声明的方法. 该信息通常由使用反射的代码使用,以使用它们来检查对象/类型或运行方法,而无需在对其本身进行编码时定义和编译类.

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.

运行时"可能会被强调,因为类定义可能会随着时间而改变,或者对象可能被声明为超类型,而该对象实际上是所声明的子类型的实例.加载某个类时,就是该实例期间加载的信息,将由getClass()方法返回.

"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.

简而言之,当您运行代码时,VM将以与您在.java文件中键入的源"形式不同的方式对类进行定义.当然,这些信息将在编译后加载,并且所有元数据(如上所述)将构成它们所谓的运行时类". 这是一种奇妙的说法,即一个对象,其中包含程序运行时加载的有关类的所有元数据"

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. 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"

这篇关于什么是“运行时类"?在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆