Java reflection mechanism example explanation

This paper analyzes the java reflection mechanism with examples. Share with you for your reference, as follows:

Reflection, I often heard them say at that time. I have seen some materials and may have used them in design patterns, but I don't feel that I have a deeper understanding of it. I learned it again this time. I feel it's OK!

1、 Let's first look at the concept of reflection:

It mainly refers to the ability of a program to access, detect and modify its own state or behavior, and adjust or modify the state and related semantics of the behavior described by the application according to the state and results of its own behavior. Reflection is a powerful tool in Java, which makes it easy for us to create flexible code. These code can be run again without source code link between components. However, improper use of reflection will cost a lot! The concept is dizzy. Continue to look down.

2、 Role of reflection mechanism:

1. Decompile: class-->. Java 2. Access the properties, methods and construction methods of Java objects through reflection mechanism; It seems easier to understand. Let's see how to implement these functions.

3、 Here, let's take a look at the classes in the reflection mechanism provided by Sun:

We can query many methods, attributes and other operations in reflection from these four classes. Or which sentence to learn to constantly query API, that is our best teacher.

4、 Specific function realization:

1. There are three ways to obtain a class through reflection mechanism. Let's get the employee type

2. Create object: after obtaining the class, we will create its object by using newinstance:

3. Get attribute: divided into all attributes and specified attributes:

① First, look at the writing method of obtaining all attributes:

② Obtain specific attributes and learn by comparing with traditional methods:

4. The acquisition method and construction method will not be described in detail. Just look at the keywords:

In this way, we can obtain various contents of the class and decompile it. For Java, a language compiled before running, reflection mechanism can make the code more flexible and easier to realize object-oriented.

5、 Reflection plus configuration file makes our program more flexible:

In the study of design patterns, reflection was used when learning the abstract factory to more easily read the database link string. At that time, I didn't understand it very well, so I copied it. to glance at. Net reflection + profile usage:

The configuration file used at that time was app Config file, the content is in XML format, and the content of the linked database is filled in:

How to write reflection:

assembly. Load ("name of the current assembly") Createinstance ("current namespace name". Class name to instantiate);

The advantage is that it is easy for us to change the database. For example, if we upgrade the system database from SQL server to Oracle, we can write two D layers and change the content of the configuration file or add conditions to select, which brings great convenience.

Of course, the same is true in Java, but the configuration file here is Properties, called the properties file. Read the contents through reflection. In this way, the code is fixed, but we can change the content of the configuration file, which makes our code much more flexible!

To sum up, learning java reflection again and using it flexibly can make our code more flexible, but it also has its disadvantages, that is, using it will reduce the performance and increase the complexity of our software, so we should use it carefully.

I hope this article will be helpful to you in Java programming.

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
分享
二维码
< <上一篇
下一篇>>