Method methodcharAt=String.class.getMethod("charAt", int.class);
methodcharAt.invoke(str,2);
代码没有错,应该是你没有抛出异常!
public class A
{
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
{ String str="abcdef";
Method methodcharAt=String.class.getMethod("charAt", int.class);
System.out.println(methodcharAt.invoke(str, 2));
}
}
|