java 反射动态调用示例
打印类Print.java
package com.test.reflct;
public class Print {
public static void print(String str){
System.out.println("打印测试"+str);
}
}
调用类
package com.test.reflct;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
try {
//加载类
Class<?> printClass = Class.forName("com.test.reflct.Print");
//获取方法
Method printMethod = printClass.getMethod("print",String.class);
//调用
printMethod.invoke(printClass.newInstance(),"123");
} catch (Exception e) {
e.printStackTrace();
}
}
}