import java.lang.reflect.Method;public class Main { public static void main(String[] args) { try { // 创建一个示例工具 MyClass obj = new MyClass(); // 获取MyClass类的Class工具 Class<?> clazz = obj.getClass(); // 获取MyClass类的方法 Method method = clazz.getDeclaredMethod("myMethod", int.class, int.class); // 获取方法的返回值类型 Class<?> returnType = method.getReturnType(); // 输出返回值类型 System.out.println("方法返回值类型: " + returnType.getName()); } catch (Exception e) { e.printStackTrace(); } }}class MyClass { public int myMethod(int a, int b) { return a + b; }}
创建了一个名为MyClass的类,个中有一个名为myMethod的方法。利用反射获取到这个方法,并调用getReturnType()方法获取它的返回值类型,末了输出返回值类型。
