package question;
import java.lang.reflect.Method;
class Test {
public void methodPublic() {
System.out.println("Test.methodPublic");
}
protected void methodProtected() {
System.out.println("Test.methodProtected");
}
private void methodPrivate() {
System.out.println("Test.methodPrivate");
}
}
public class PrivateTest {
public static void main(String[] args) throws Exception {
Test test = new Test();
callMethods(test, "methodPublic");
callMethods(test, "methodProtected");
callMethods(test, "methodPrivate");
}
static void callMethods(Object a, String methodName) throws Exception {
Method m = a.getClass().getDeclaredMethod(methodName);
m.setAccessible(true);
m.invoke(a);
}
}
Login in to like
Login in to comment