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

What does the reflection mechanism in Java mean?

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

Share

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

This article is about what the reflection mechanism in Java means. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Today we are going to talk about the reflection mechanism of java.

When a program actively uses a class, if the class has not been loaded into memory, the system initializes the class through the three steps of loading, connecting, and initializing. Sometimes the whole process is called a class loader or class initialization.

The value when the class is loaded reads the class file into memory and creates an object for the class, which can then directly call the class and its implemented parent class methods and properties

First, what is the reflection mechanism: a class is composed of a group of objects (with common attributes), and there are common properties, constructors, methods and other structures between classes, which are used to describe the characteristics of the class. that is, you can call or view the properties, methods, that is, constructors of a class is called reflection.

Second, the benefits of java reflection mechanism:

1. It greatly improves the expansibility of the application, which is realized by passing the subclass object to the parent class application, which greatly increases the coupling of the code, and can not access the information of the real object. At this time, we can use the reflection mechanism.

two。 When the code needs to be maintained later, it needs to be modified in many places, while through the reflection mechanism, only one place needs to be modified directly.

E.g Person person=new Student ()

Third, application scenarios: generally applied to the underlying framework applications, but most of them have been packaged, and can implement design patterns such as factory patterns and proxy patterns, and can also solve distressing problems such as java generic erasure

The composition of a class:

Member variables, member methods, constructors, modifiers, packages, etc.

Fourth, the composition of reflection: 1.Class (class itself) 2.Field (attribute) 3.Method (method)

Let's go straight to the code phase:

First, there are three ways to get a class:

Person person = new Person ()

1.Class clazz = Person.class;// gets the object through the class

2.Class clazz = Class.forName ("Reflect.Person"); / / through the class permission name (package name. Class name) to get the Class object

/ / Class.forName throws an exception because the class is found according to the string, so such a class exception may not be found

3.Class clazz = person.getClass (); / / call through the class object

Second, self-category

Class [] interfaces = clazz.getInterfaces (); / / get all the interfaces in the Person class

String classname = clazz.getName (); / / get the class name (package name. Class name)

String simpleName = clazz.getSimpleName (); / / just get the class name, not the package name

Int modifiers = clazz.getModifiers (); / / what is the permission character? the return value is int 1:public 2:private in turn.

Package aPackage = clazz.getPackage (); / / get the package name

Class superclass = clazz.getSuperclass (); / / gets the parent class of this class, which has one and only one

Object o = clazz.newInstance (); / / generate the instance object of the class, because you don't know what kind of object to generate, and the top of all classes is the subclass of Object. Which hospital in Zhengzhou has a good flow of people?

III. Attributes (Filed)

Field math = clazz.getField ("math"); / / get the specified name attribute, and can only find the public attribute

Field [] fields = clazz.getFields (); / / get all the attributes of public and the parent class

Field id = clazz.getDeclaredField ("id"); / / get public and specified (id) attributes such as private, protected, etc.

Field [] declaredFields = clazz.getDeclaredFields (); / / get all attributes and parent classes

Of course, to find out what the performance is doing, the most important thing is to assign values.

Because of the private property, no class other than this class can access it, so the setting value is naturally different from that of public.

The public property sets the value:

Field name = clazz.getField ("name")

Name.set (o, "TTL"); / / the first property is to set which class the worthwhile property belongs to in the instantiated object, and the second parameter is the value to be set

Private property settings:

Field sex = clazz.getDeclaredField ("sex")

Sex.setAccessible (true); / / set access permissions

Sex.set (person1, "female")

IV. Methods

Method setAddress = clazz.getMethod ("Address", String.class)

/ / the first parameter is the name of the method to be found, and the second parameter is the type of formal parameter of the method to be found.

Object address = setAddress.invoke (person1, "Suzhou")

/ / the first parameter is the object instantiated by the class, and the second parameter is the value to be set by the parameter

/ / execution method

Fifth, construction method

Constructor constructor = clazz.getConstructor (String.class, String.class)

/ / since the constructor is the same as the class name, there is no need to write the class name object here, only the parameters can be written to match the corresponding constructor

Constructor.newInstance ("TTL", "Sichuan"); / / execute the construction method

Thank you for reading! This is the end of this article on "what is the meaning of reflection mechanism in Java?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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