public class TestInvokeMethod {
private void run() {
System.out.println("方法测试");
}
private void run2(int n){
System.out.println("方法测试"+n);
}
}
public class Test {
public static void main(String[] args) throws Exception {
//反射
Class clazz=TestInvokeMethod.class;
Object obj=clazz.newInstance();
Method method=clazz.getDeclaredMethod("run",null);
method.setAccessible(true);
method.invoke(obj, null);
Method method2=clazz.getDeclaredMethod("run2",new Class[]{int.class});
method2.setAccessible(true);
method2.invoke(obj, 2);
}
}