import java.lang.reflect.Method;
public class Demo {
public void runString(String str){
System.out.println(str);
}
public void runInteger(Integer i){
System.out.println(i);
}
public static String getDataType(String dataType){
switch (dataType){
case "int":
return "runInteger";
case "String":
return "runString";
}
return "";
}
public static Class getDataClass(String dataType){
switch (dataType){
case "int":
return Integer.class;
case "String":
return String.class;
}
return null;
}
public static void main(String[] args){
runMethod("String","30");
}
public static void runMethod(String dataType,Object param ) {
Demo demo = new Demo();
Class clazz = demo.getClass();
try {
Method method = clazz.getMethod(getDataType(dataType),getDataClass(dataType));
method.invoke(demo,param);
} catch (Exception e) {
e.printStackTrace();
}
}
}
动态替换方法名Demo
最新推荐文章于 2021-03-08 11:07:31 发布