import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Person person = new Person();
Method privateMethod = person.getClass().getDeclaredMethod("privateMethod");
// 设置私有方法可访问
privateMethod.setAccessible(true);
// 调用私有方法
privateMethod.invoke(person);
}
}
class Person {
private void privateMethod() {
System.out.println("调用了私有方法");
}
}