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();
}
}
}
标签:reflct,调用,java,String,示例,Print,printClass,public 来源: https://www.cnblogs.com/littlewrong/p/15343694.html