用户:xuebincui
2017年12月20日
public class B {
public void m(int x){
System.out.println("m function");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
package a;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//import b.B;
public class A {
/**
* @param args
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
// TODO Auto-generated method stub
// B b = new B();
Class c = Class.forName("b.B");
Object yourObj = c.newInstance();
Method sAge = c.getMethod("m", new Class[] {int.class});
Object[] arguments = new Object[] { new Integer(37)};
//执行方法
sAge.invoke(yourObj , arguments);
}
}
不可以,可以使用公用的方法,反射机制不能调用私有方法。