Java 反射
Java 反射
在本教程中,我们将学习反射,这是 Java 编程中的一个功能,可以让我们检查和修改类、方法等。
在 Java 中,反射允许我们在运行时检查和操作类、接口、构造函数、方法和字段。
Java中有一个名为Class
的类 在运行时保留有关对象和类的所有信息。 Class 的对象 可以用来进行反射。
Java类的反射
为了体现一个Java类,我们首先需要创建一个Class的对象 .
而且,使用该对象,我们可以调用各种方法来获取有关类中存在的方法、字段和构造函数的信息。
Class 对象的创建方式有以下三种:
1。使用 forName() 方法
class Dog {...}
// create object of Class
// to reflect the Dog class
Class a = Class.forName("Dog");
这里,forName()
方法将要反映的类的名称作为其参数。
2。使用 getClass() 方法
// create an object of Dog class
Dog d1 = new Dog();
// create an object of Class
// to reflect Dog
Class b = d1.getClass();
在这里,我们使用 Dog 的对象 类来创建 Class 的对象 .
3。使用 .class 扩展名
// create an object of Class
// to reflect the Dog class
Class c = Dog.class;
现在我们知道了如何创建 Class
的对象 .我们可以使用这个对象在运行时获取对应类的信息。
示例:Java 类反射
import java.lang.Class;
import java.lang.reflect.*;
class Animal {
}
// put this class in different Dog.java file
public class Dog extends Animal {
public void display() {
System.out.println("I am a dog.");
}
}
// put this in Main.java file
class Main {
public static void main(String[] args) {
try {
// create an object of Dog
Dog d1 = new Dog();
// create an object of Class
// using getClass()
Class obj = d1.getClass();
// get name of the class
String name = obj.getName();
System.out.println("Name: " + name);
// get the access modifier of the class
int modifier = obj.getModifiers();
// convert the access modifier to string
String mod = Modifier.toString(modifier);
System.out.println("Modifier: " + mod);
// get the superclass of Dog
Class superClass = obj.getSuperclass();
System.out.println("Superclass: " + superClass.getName());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
输出
Name: Dog Modifier: public Superclass: Animal
在上面的例子中,我们创建了一个超类:Animal 和一个子类:Dog .在这里,我们试图检查类 Dog .
注意声明,
Class obj = d1.getClass();
在这里,我们正在创建一个对象 obj 类 使用 getClass()
方法。使用该对象,我们正在调用 Class 的不同方法 .
- obj.getName() - 返回类的名称
- obj.getModifiers() - 返回类的访问修饰符
- obj.getSuperclass() - 返回类的超类
了解更多关于 Class
,访问Java Class(Java官方文档)。
注意 :我们使用的是 Modifier
类将整数访问修饰符转换为字符串。
反映字段、方法和构造函数
包java.lang.reflect
提供可用于操作类成员的类。例如,
- 方法类 - 提供有关类中方法的信息
- 字段类 - 提供有关类中字段的信息
- 构造函数类 - 提供有关类中构造函数的信息
1. Java 方法的反射
Method
class 提供了各种方法,可用于获取有关类中存在的方法的信息。例如,
import java.lang.Class;
import java.lang.reflect.*;
class Dog {
// methods of the class
public void display() {
System.out.println("I am a dog.");
}
private void makeSound() {
System.out.println("Bark Bark");
}
}
class Main {
public static void main(String[] args) {
try {
// create an object of Dog
Dog d1 = new Dog();
// create an object of Class
// using getClass()
Class obj = d1.getClass();
// using object of Class to
// get all the declared methods of Dog
Method[] methods = obj.getDeclaredMethods();
// create an object of the Method class
for (Method m : methods) {
// get names of methods
System.out.println("Method Name: " + m.getName());
// get the access modifier of methods
int modifier = m.getModifiers();
System.out.println("Modifier: " + Modifier.toString(modifier));
// get the return types of method
System.out.println("Return Types: " + m.getReturnType());
System.out.println(" ");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
输出
Method Name: display Modifier: public Return Types: void Method Name: makeSound Modifier: private Return Types: void
在上面的示例中,我们试图获取有关 Dog 中存在的方法的信息 班级。如前所述,我们首先创建了一个对象 obj Class
使用 getClass()
方法。
注意表达式,
Method[] methods = obj.getDeclaredMethod();
这里,getDeclaredMethod()
返回类中存在的所有方法。
另外,我们创建了一个对象 m Method
班级。在这里,
- m.getName() - 返回方法的名称
- m.getModifiers() - 以整数形式返回方法的访问修饰符
- m.getReturnType() - 返回方法的返回类型
Method
类还提供了各种其他方法,可用于在运行时检查方法。要了解更多信息,请访问 Java Method 类(Java 官方文档)。
2. Java 字段的反射
和方法一样,我们也可以使用 Field
的方法检查和修改一个类的不同字段 班级。例如,
import java.lang.Class;
import java.lang.reflect.*;
class Dog {
public String type;
}
class Main {
public static void main(String[] args) {
try {
// create an object of Dog
Dog d1 = new Dog();
// create an object of Class
// using getClass()
Class obj = d1.getClass();
// access and set the type field
Field field1 = obj.getField("type");
field1.set(d1, "labrador");
// get the value of the field type
String typeValue = (String) field1.get(d1);
System.out.println("Value: " + typeValue);
// get the access modifier of the field type
int mod = field1.getModifiers();
// convert the modifier to String form
String modifier1 = Modifier.toString(mod);
System.out.println("Modifier: " + modifier1);
System.out.println(" ");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
输出
Value: labrador Modifier: public
在上面的例子中,我们创建了一个名为 Dog 的类 .它包括一个名为 type 的公共字段 .注意声明,
Field field1 = obj.getField("type");
在这里,我们正在访问 Dog 的公共字段 类并将其分配给对象 field1 字段 类。
然后我们使用了 Field
的各种方法 类:
- field1.set() - 设置字段的值
- field1.get() - 返回字段的值
- field1.getModifiers() - 以整数形式返回字段的值
同样,我们也可以访问和修改私有字段。但是,私有域的反射与公共域的反射略有不同。例如,
import java.lang.Class;
import java.lang.reflect.*;
class Dog {
private String color;
}
class Main {
public static void main(String[] args) {
try {
// create an object of Dog
Dog d1 = new Dog();
// create an object of Class
// using getClass()
Class obj = d1.getClass();
// access the private field color
Field field1 = obj.getDeclaredField("color");
// allow modification of the private field
field1.setAccessible(true);
// set the value of color
field1.set(d1, "brown");
// get the value of field color
String colorValue = (String) field1.get(d1);
System.out.println("Value: " + colorValue);
// get the access modifier of color
int mod2 = field1.getModifiers();
// convert the access modifier to string
String modifier2 = Modifier.toString(mod2);
System.out.println("Modifier: " + modifier2);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
输出
Value: brown Modifier: private
在上面的例子中,我们创建了一个名为 Dog 的类 .该类包含一个名为 color 的私有字段 .注意声明。
Field field1 = obj.getDeclaredField("color");
field1.setAccessible(true);
在这里,我们正在访问 color 并将其分配给对象 field1 Field
班级。然后我们使用 field1 修改 color 的可访问性 并允许我们对其进行更改。
然后我们使用 field1 对私有字段颜色进行各种操作。
详细了解 Field 的不同方法 ,访问Java Field Class(Java官方文档)。
3。 Java构造函数的反射
我们还可以使用 Constructor
提供的各种方法检查类的不同构造函数 班级。例如,
import java.lang.Class;
import java.lang.reflect.*;
class Dog {
// public constructor without parameter
public Dog() {
}
// private constructor with a single parameter
private Dog(int age) {
}
}
class Main {
public static void main(String[] args) {
try {
// create an object of Dog
Dog d1 = new Dog();
// create an object of Class
// using getClass()
Class obj = d1.getClass();
// get all constructors of Dog
Constructor[] constructors = obj.getDeclaredConstructors();
for (Constructor c : constructors) {
// get the name of constructors
System.out.println("Constructor Name: " + c.getName());
// get the access modifier of constructors
// convert it into string form
int modifier = c.getModifiers();
String mod = Modifier.toString(modifier);
System.out.println("Modifier: " + mod);
// get the number of parameters in constructors
System.out.println("Parameters: " + c.getParameterCount());
System.out.println("");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
输出
Constructor Name: Dog Modifier: public Parameters: 0 Constructor Name: Dog Modifier: private Parameters: 1
在上面的例子中,我们创建了一个名为 Dog 的类 .该类包括两个构造函数。
我们正在使用反射来查找有关类的构造函数的信息。注意声明,
Constructor[] constructors = obj.getDeclaredConstructor();
在这里,我们正在访问 Dog 中存在的所有构造函数 并将它们分配给数组 constructors Constructor
输入。
然后我们使用对象 c 获取构造函数的不同信息。
- c.getName() - 返回构造函数的名称
- c.getModifiers() - 以整数形式返回构造函数的访问修饰符
- c.getParameterCount() - 返回每个构造函数中存在的参数数量
了解更多Constructor
的方法 类,访问构造函数类
java