package reflection; import java.lang.reflect.Method; public class MethodInvokeTest { public int add(int a,int b) { return a+b; } public String echo(String mgs) { return "echo is: " + mgs; } public static void main(String[] args) throws Exception{ Class classType = MethodInvokeTest.class; Object object = classType.newInstance(); //反射调用前面的add方法 Method addMethod = classType.getMethod("add", new Class[]{int.class,int.class}); Object result = addMethod.invoke(object, new Object[]{new Integer(100),new Integer(200)}); System.out.println("add 方法调用的结果是:" + Integer.parseInt(result.toString())); //反射调用前面的echo方法 Method echoMethod = classType.getMethod("echo", new Class[]{String.class}); result = echoMethod.invoke(object, new Object[]{"Hello,linwei!"}); System.out.println("echo 方法调用的结果是:" + result); } }