public class MethodDemo {
/**
* @param args
* @throws Exception
* @throws SecurityException
* @throws NoSuchMethodException
*/
public static void main(String[] args) throws NoSuchMethodException, SecurityException, Exception {
//第一种调用方法
MethodDemo2.main(new String[]{"q","c"});
//method方法
String className=args[0]; //这个className为什么是args[],应该改成"MethodDemo2"吧
Method mainMethod=Class.forName(className).getMethod("main", String.class); //这个地方应该是String[].class
mainMethod.invoke(null,(Object)new String[]{"q","c"});
}
}
class MethodDemo2{
public static void main(String[] args){
for(String arg:args){
System.out.println(arg);
}
}
}
测完运行结果是
q
c
q
c |