//测试方法
@Test
public void test01() {
Class p = null;
Person01 person01 = new Person01();
try {
p = Class.forName("pojo.Person01");
// 第一个参数是类的方法名 第二个是需要传的参数
Method method = p.getMethod("returnName", String.class);
String aa = (String) method.invoke(person01, "123");
Method method2 = p.getMethod("returnId", Integer.class);
String bb = (String) method2.invoke(person01, 123);
// 多个参数
Class [] argTypes = new Class[2];
argTypes[0]= String.class;
argTypes[1] = Integer.class;
Method methods = p.getDeclaredMethod("getDouble",Integer.class,String.class);
Object [] objects = new Object[2];
objects[0] = "test";
objects[1] = 123;
String cc = (String) methods.invoke(person01,123,"123");
System.out.println(aa);
System.out.println(bb);
System.out.println(cc);
} catch (Exception e) {
e.printStackTrace();
}
}