- public class MethodDemo {
- public static void main(String[] args) throws Exception{
- String str="abc";
- Method charAtMethod=String.class.getMethod("charAt", int.class);
- System.out.println(charAtMethod.invoke(str, 2));
-
- String classStartingName = args[0];
-
- Method mainMethod = Class.forName(classStartingName).getMethod("main", String[].class);
-
- //方式一:强制转换为超类Object,不用拆包
- mainMethod.invoke(null, (Object)new String[]{"111","222","333"});
-
- //方式二:将数组打包,编译器拆包后就是一个String[]类型的整体
- // methodMain.invoke(null, new Object[]{new String[]{"111","222","333"}});
- }
- }
- class MethodTestDemo{
-
- public static void main(String[] args) {
- for (String str : args){
-
- System.out.println(str);
- }
- }
- }
复制代码 |
|