检查类是什么意思

huangapple go评论67阅读模式
英文:

What is meant by Inspecting classes

问题

我正在学习 Java 中的反射,它的主要目的是在运行时检查类。

能告诉我检查类的含义是什么吗?

英文:

Im going through Reflection in java and its main purpose is to inspect classes at RunTime.

May I know what does inspecting classes mean ?

答案1

得分: 1

检查意味着查找类具有哪些字段和方法。这些方法的签名是什么。方法、字段和类本身上有哪些注释。

这对于某些类型的基础结构代码是必要的。例如,一个库接受任意类的实例(该库的作者事先对该类一无所知),并将其保存到数据库中。它需要知道类具有哪些字段,以便将它们保存到数据库中的相应列中。

英文:

Inspecting means finding out what fields and methods class has. What are signatures of those methods. What annotations are present on the methods, fields and class itself.

This is needed for some type of infrastructural code. For example a library that takes an instance of an arbitrary class (that is the class the the author of the library know nothing about beforehand) and saves it into database. It needs to know what fields the class has to save them to appropriate columns in the database.

答案2

得分: 1

你可以获取任何 Java 对象或类的 Class 对象,就像这样:

Class cls = String.class;

或者:

Class cls = "abcd".getClass();

在这两种情况下,cls 都会为您提供对 String 类型的 Class 对象的引用。

然后,您可以在该 Class 对象上调用方法,以了解有关原始对象或类的信息,例如其方法、字段和注解等。请查阅 Class 的文档

还有一系列表示类各部分的类,比如 MethodConstructorField。您也可以在 Java 文档中查找这些内容。

英文:

You can get a Class object for any Java object or class, like this:

Class cls = String.class

or:

Class cls = "abcd".getClass()

In each case here, cls gives you a reference to the String type's Class object.

You can then call methods on that Class object to learn things about the original object or class, like about its methods, fields and annotations. Take a look at the documentation of Class.

There is a series of classes representing the parts of a class, like Method, Constructor and Field. You can look those up in the Java docs as well.

huangapple
  • 本文由 发表于 2020年9月23日 00:18:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64013763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定