Method getMethod = c.getMethod(getMethodName);
我要是有两个相同名称,不同返回值的类型不同,这时候根据方法名获取的方法,就有两个方法了啊,就是说类中出现了重载,怎么办?
在括号里方法名后边,在填一个实参,对应该方法的类型就行了
Demo mDemo = new Demo("abc");
Class c = mDemo.getClass();
Method method = c.getMethod("putOut", String.class);
String string = (String) method.invoke(mDemo, "123");
System.out.println(string);测试代码,可以通过
public transient Method getMethod(String s, Class aclass[])
throws NoSuchMethodException, SecurityException
{
checkMemberAccess(0, Reflection.getCallerClass(), true);
Method method = getMethod0(s, aclass);
if(method == null)
throw new NoSuchMethodException((new StringBuilder()).append(getName()).append(".").append(s).append(argumentTypesToString(aclass)).toString());
else
return method;
}哦,看了一下getMethod方法,才知道,后面可以跟参数列表类型