Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the principle of Java reflection Mechanism

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the introduction of the principle of Java reflection mechanism". In the daily operation, I believe that many people have doubts about the introduction of the principle of Java reflection mechanism. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "introduction to the principle of Java reflection mechanism". Next, please follow the editor to study!

1. What is the Java reflection mechanism

The reflection mechanism of Java is that it can fully know any class, and other properties and methods when the program is running, and can call the properties and methods of an object arbitrarily. This dynamic acquisition of the runtime is the reflection mechanism of Java. In fact, this is also a symbol of Java as a dynamic language.

To sum up reflection in one sentence is to load a class that is only known at runtime and its complete internal structure.

two。 Why should there be Java reflection mechanism?

Why should we use Java's reflection mechanism?

I think there are two kinds:

* species: the purpose of reflection is to expand unknown applications. For example, if you write a program that defines some interfaces, as long as the dll that implements these interfaces can be inserted into the program as a plug-in. So how to achieve it? It can be done through reflection. Is to load the dll into memory and then call the methods in the dll by reflection.

Second: you don't know that class name during the coding phase, so if you want to read the class name from the configuration file at run time, there is no way to hard-code new ClassName (). Instead, you have to use reflection to create this object.

A common example in life helps to understand why we use Java's reflection mechanism: when you go into a restaurant, you don't know what kind of food they all have and how much they cost. So what are you doing? "waiter, bring me a menu." then point to the menu and say, "I'll take this, I'll take that."

3. Let's take a look at ReflectionAPI

In life, when we use an unknown thing, we always use help to solve our problems. TV is helpful, computer is helpful, almost everything carries its help, and the reflection mechanism of Java is no exception.

With the help of Reflection API in JDK, it mainly explains what the Java reflection mechanism is, what properties and methods this reflection mechanism provides, and further we can know what kind of work can be done through it.

Let's take a look at this part of the API. These interfaces and classes are located in the lang package.

As shown in the figure:

Interface:

Class:

Briefly introduce the use of some classes and interfaces:

1) A Member member is an interface that reflects identification information about a single member (field or method) or constructor

2) InvocationHandler is the interface implemented by the call handler of the proxy instance (the specific usage of this interface wait until the analysis of the java reflection mechanism 4 focuses on)

3) Method provides information about the methods of a class and an interface to access the methods of the class.

Example:

Import java.lang.reflect.Method; public class TestMethod {/ * @ param args * @ throws Exception * / public static void main (String [] args) throws Exception {/ / TODO Auto-generated method stub Class classType=Class.forName (args [0]); Method methods [] = classType.getDeclaredMethods (); for (int iTuno [I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report