Analysis of java reflection mechanism

Reflection, that is, reflection, is an important feature of the Java language. We know that before using a class, we often have created it, such as creating a class file, and then writing some properties and methods. That is, this kind is static, but the reflection mechanism allows you to create a class dynamically. In addition to dynamically creating a class, we can also dynamically obtain the data of similar objects and assign these data to the newly created class, which is a bit similar to clone replication. In many cases, we need this feature of dynamically creating classes. For example, when processing some businesses, but these businesses are slightly different, they often correspond to multiple classes. When processing, we need to call different classes according to different business processes. At this time, the reflection mechanism comes in handy. The following is about the software package Java in the JDK API Lang. reflect Description: provides classes and interfaces to obtain reflection information about classes and objects. Within security limits, reflection allows programmatic access to information about the fields, methods, and constructors of the loaded class, and allows the use of reflection fields, methods, and constructors to operate on basic peers on objects. Accessibleobject allows access checks to be suppressed if the required reflectpermission is available. Arrays provides static methods for dynamically creating and accessing arrays. The classes in this package and Java Lang. class can meet the needs of the following applications: debugger, interpreter, object checker, class browser, and services (such as object serialization and JavaBean, which need to access the public members of the target object (based on its runtime class) or the members declared by a given class).

Here are two simple examples to illustrate the use of reflection. First, create a person class:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>