这是一个使用涉及原语的反射调用方法的简单示例。
import java.lang.reflect.*;public class ReflectionExample { public int test(int i) { return i + 1; } public static void main(String args[]) throws Exception { Method testMethod = ReflectionExample.class.getMethod("test", int.class); int result = (Integer) testMethod.invoke(new ReflectionExample(), 100); System.out.println(result); // 101 }}要健壮,应该捕获和处理所有检查反射有关的异常
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException。



