package com;
public class Person {
private String name;
private int age;
public Person() {
this.name = "Unknown";
this.age = 0;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void introduce() {
System.out.println("Hi, I'm " + name + " and I'm " + age + " years old.");
}
private void secretMethod() {
System.out.println("This is a secret method!");
}
}
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) {
try {
// 通过类名获取 Person 类的 Class 对象
Class<?> personClass = Class.forName("com.Person");
System.out.println("类名: " + personClass.getName());
// 使用 Class 对象的 newInstance() 方法创建 Person 实例
Object person1 = personClass.newInstance();
System.out.println("使用newInstance()创建的实例:");
// 获取 introduce 方法的 Method 对象
Method introduceMethod = personClass.getMethod("introduce");
// 调用 person1 的 introduce 方法
introduceMethod.invoke(person1);
// 获取带有 String 和 int 参数的构造函数
Constructor<?> constructor = personClass.getConstructor(String.class, int.class);
// 使用获取的构造函数创建新的 Person 实例
Object person2 = constructor.newInstance("爱丽丝", 30);
System.out.println("\n使用构造函数创建的实例:");
// 调用 person2 的 introduce 方法
introduceMethod.invoke(person2);
// 获取 Person 类的所有公共方法
Method[] methods = personClass.getMethods();
System.out.println("\n所有公共方法:");
for (Method method : methods) {
System.out.println(method.getName());
}
// 获取 Person 类声明的所有方法,包括私有方法
Method[] declaredMethods = personClass.getDeclaredMethods();
System.out.println("\n所有声明的方法(包括私有方法):");
for (Method method : declaredMethods) {
System.out.println(method.getName());
}
// 获取名为 "secretMethod" 的私有方法
Method secretMethod = personClass.getDeclaredMethod("secretMethod");
// 设置该方法为可访问,即使它是私有的
secretMethod.setAccessible(true);
System.out.println("\n调用私有方法:");
// 在 person2 实例上调用 secretMethod 方法
secretMethod.invoke(person2);
// 获取 Person 类声明的所有字段,包括私有字段
Field[] fields = personClass.getDeclaredFields();
System.out.println("\n所有声明的字段:");
for (Field field : fields) {
System.out.println(field.getName());
}
// 获取名为 "name" 的私有字段
Field nameField = personClass.getDeclaredField("name");
// 设置该字段为可访问,即使它是私有的
nameField.setAccessible(true);
System.out.println("\n更改私有字段值:");
// 修改 person2 实例的 name 字段值
nameField.set(person2, "鲍勃");
// 再次调用 introduce 方法,查看更改后的结果
introduceMethod.invoke(person2);
} catch (ClassNotFoundException e) {
// 当找不到指定的类时捕获此异常
System.out.println("未找到类: " + e.getMessage());
} catch (InstantiationException | IllegalAccessException e) {
// 当无法实例化类或无法访问类的构造函数时捕获这些异常
System.out.println("创建实例时出错: " + e.getMessage());
} catch (NoSuchMethodException e) {
// 当找不到指定的方法时捕获此异常
System.out.println("未找到方法: " + e.getMessage());
} catch (InvocationTargetException e) {
// 当被调用的方法本身抛出异常时捕获此异常
System.out.println("调用方法时出错: " + e.getMessage());
} catch (NoSuchFieldException e) {
// 当找不到指定的字段时捕获此异常
System.out.println("未找到字段: " + e.getMessage());
}
}
}
这些注释详细解释了代码的每个部分,包括:
- 如何使用反射获取类的信息
- 如何创建类的实例
- 如何获取和调用方法(包括私有方法)
- 如何获取和修改字段(包括私有字段)
- 异常处理的目的
通过这些注释,您可以更好地理解Java反射API的使用方法,以及如何通过反射来操作类、方法和字段。
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/113ed826b34c4b488754d0986fd404e4.png#pic_center