Home > Java > javaTutorial > Basic principles and calling methods of Java reflection

Basic principles and calling methods of Java reflection

王林
Release: 2023-12-23 09:01:07
Original
1140 people have browsed it

Basic principles and calling methods of Java reflection

Basic principles and calling methods of Java reflection

Foreword:
Java reflection is an important feature in the Java language, which allows the program to dynamically Obtain class information and operate class members. Through reflection, we can dynamically create objects, call methods, get/set properties, etc. at runtime, which greatly improves the flexibility and scalability of the program. This article will introduce the basic principles of Java reflection and give specific code examples.

1. Basic principles of reflection
The implementation of Java reflection is based on the Class class, which is the core class of Java reflection. Each Java class will generate a corresponding Class object after compilation. Class information can be obtained through the Class object, and class operations can be performed through the class information.

Java reflection mainly has the following core classes and interfaces:

  1. Class class: represents the type of class, which is the entry point of Java reflection.
  2. Field class: represents the member variables of the class.
  3. Method class: represents the member method of the class.
  4. Constructor class: Represents the constructor method of the class.

The basic principle of Java reflection is as follows: first, obtain the corresponding Class object through the fully qualified name of the class or the getClass() method of the object; then, through some methods of the Class class, such as getFields() , getMethods(), etc., to obtain Field, Method, Constructor and other objects; finally, perform specific operations through these objects, such as getting/setting attribute values, calling methods, creating objects, etc.

2. Reflection calling method
2.1 Obtain Class object
The corresponding Class object can be obtained through the fully qualified name of the class or the getClass() method of the object.

The sample code is as follows:

// 通过类的全限定名获取Class对象
Class<?> clazz1 = Class.forName("com.example.User");

// 通过对象的getClass()方法获取Class对象
User user = new User();
Class<?> clazz2 = user.getClass();
Copy after login

2.2 Obtaining the attribute value
The attribute value of the object can be obtained through the get() method of the Field class.

The sample code is as follows:

// 获取public属性值
Field field = clazz.getDeclaredField("name");
String name = (String) field.get(user);

// 获取private属性值
Field privateField = clazz.getDeclaredField("age");
privateField.setAccessible(true); // 设置private属性的访问权限
int age = (int) privateField.get(user);
Copy after login

2.3 Setting the attribute value
The attribute value of the object can be set through the set() method of the Field class.

The sample code is as follows:

// 设置public属性值
Field field = clazz.getDeclaredField("name");
field.set(user, "Tom");

// 设置private属性值
Field privateField = clazz.getDeclaredField("age");
privateField.setAccessible(true); // 设置private属性的访问权限
privateField.set(user, 20);
Copy after login

2.4 Calling the method
The method of the object can be called through the invoke() method of the Method class.

The sample code is as follows:

// 调用无参方法
Method method1 = clazz.getDeclaredMethod("sayHello");
method1.invoke(user);

// 调用带参方法
Method method2 = clazz.getDeclaredMethod("sayHi", String.class);
method2.invoke(user, "Jack");
Copy after login

2.5 Creating objects
Objects can be created through the newInstance() method of the Constructor class.

The sample code is as follows:

Constructor<?> constructor = clazz.getDeclaredConstructor(String.class, int.class);
User newUser = (User) constructor.newInstance("Lucy", 25);
Copy after login

Summary:
Java reflection is a powerful feature in the Java language, which can dynamically obtain class information and operate class members at runtime . The basic principle of reflection is to obtain class information through the Class class and perform specific operations through Field, Method, Constructor and other objects. In actual applications, we can use the reflection mechanism to realize various dynamic requirements and improve the flexibility and scalability of the program.

The above is the detailed content of Basic principles and calling methods of Java reflection. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template