Java反射机制可以在程序运行时动态获取类的结构信息,其中获取对象的方法是反射的常见使用场景,通过相关API可以拿到类定义的所有方法并完成后续调用操作。

反射获取对象方法的核心API
Java反射中获取对象方法主要依赖Class类的两个核心方法,二者的使用场景和返回结果有明显区别:
- getMethods():返回当前类及其所有父类、父接口中的公开(public)方法,返回类型为
Method[]数组。 - getDeclaredMethods():仅返回当前类自身定义的所有方法,包括私有(private)、受保护(protected)、默认访问权限和公开方法,不包含继承的方法,返回类型同样为
Method[]数组。
获取公开方法的示例
以下示例定义一个父类和一个子类,演示getMethods()的使用效果:
// 父类
class Parent {
public void parentPublicMethod() {
System.out.println("父类公开方法");
}
private void parentPrivateMethod() {
System.out.println("父类私有方法");
}
}
// 子类
class Child extends Parent {
public void childPublicMethod() {
System.out.println("子类公开方法");
}
private void childPrivateMethod() {
System.out.println("子类私有方法");
}
}
public class ReflectGetMethodDemo {
public static void main(String[] args) {
Class<Child> childClass = Child.class;
// 获取所有公开方法,包含父类的公开方法
Method[] publicMethods = childClass.getMethods();
System.out.println("getMethods()获取的方法列表:");
for (Method method : publicMethods) {
System.out.println(method.getName());
}
}
}
上述代码执行后,输出的方法会包含childPublicMethod、parentPublicMethod,同时还会包含从Object类继承的toString、hashCode等公开方法,不会包含任何私有方法。
获取所有自定义方法的示例
如果需要获取类自身定义的所有方法,包括私有方法,需要使用getDeclaredMethods():
public class ReflectGetDeclaredMethodDemo {
public static void main(String[] args) {
Class<Child> childClass = Child.class;
// 获取当前类自身定义的所有方法,不包含继承的方法
Method[] declaredMethods = childClass.getDeclaredMethods();
System.out.println("getDeclaredMethods()获取的方法列表:");
for (Method method : declaredMethods) {
System.out.println(method.getName());
}
}
}
上述代码执行后,输出的方法仅包含childPublicMethod和childPrivateMethod,不会包含父类的方法,也不会包含Object类继承的方法。
获取指定参数的单个方法
如果只需要获取某一个指定方法,可以通过带参数的getMethod或getDeclaredMethod方法,参数为方法名和方法的参数类型列表:
public class ReflectGetSingleMethodDemo {
public static void main(String[] args) throws Exception {
Class<Child> childClass = Child.class;
// 获取无参数的childPublicMethod公开方法
Method publicMethod = childClass.getMethod("childPublicMethod");
System.out.println("获取到的公开方法名:" + publicMethod.getName());
// 获取带参数的私有方法,假设Child类有方法 private void test(int a, String b)
// Method privateMethod = childClass.getDeclaredMethod("test", int.class, String.class);
// System.out.println("获取到的私有方法名:" + privateMethod.getName());
}
}
通过反射调用获取到的方法
通过反射获取到Method对象后,可以通过invoke方法完成方法调用,调用私有方法前需要先设置访问权限:
public class ReflectInvokeMethodDemo {
public static void main(String[] args) throws Exception {
Child child = new Child();
Class<? extends Child> childClass = child.getClass();
// 调用公开方法
Method publicMethod = childClass.getMethod("childPublicMethod");
publicMethod.invoke(child);
// 调用私有方法
Method privateMethod = childClass.getDeclaredMethod("childPrivateMethod");
// 设置私有方法可访问
privateMethod.setAccessible(true);
privateMethod.invoke(child);
}
}
反射获取方法的注意事项
- 获取私有方法时必须使用
getDeclaredMethod或getDeclaredMethods,否则会抛出NoSuchMethodException。 - 调用私有方法前必须调用
setAccessible(true),否则会抛出IllegalAccessException。 - 反射调用方法的性能比直接调用低,不要在高频执行的代码逻辑中大量使用反射调用。
- 方法参数类型传递必须准确,比如基本类型要传
int.class而不是Integer.class,否则会匹配不到对应方法。
Java反射getDeclaredMethodsgetMethodsMethod对象invoke方法修改时间:2026-06-19 04:12:30