import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) throws Exception{
//1.需要提供class的name
//2.需要提供方法名称
//3.需要提供方法参数类型;
//4.参数值
//如果使用下面的这种需要提供无参构造
String className = "T";
String paramClass = "java.lang.String";
String methodName = "say";
String paramValue = "jack";
Class<?> c = Class.forName(className);
Object obj = c.newInstance();
Method method = c.getDeclaredMethod(methodName, new Class[]{Class.forName(paramClass)});
method.invoke(obj, new Object[]{paramValue});
}
}
class T{
public void say(String name){
System.out.println("hello " + name);
}
}
public class Test {
public static void main(String[] args) throws Exception{
//1.需要提供class的name
//2.需要提供方法名称
//3.需要提供方法参数类型;
//4.参数值
//如果使用下面的这种需要提供无参构造
String className = "T";
String paramClass = "java.lang.String";
String methodName = "say";
String paramValue = "jack";
Class<?> c = Class.forName(className);
Object obj = c.newInstance();
Method method = c.getDeclaredMethod(methodName, new Class[]{Class.forName(paramClass)});
method.invoke(obj, new Object[]{paramValue});
}
}
class T{
public void say(String name){
System.out.println("hello " + name);
}
}