package test优快云;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class getPrivateMethod extends TestParent{
public void getPriMethod(){
Class c = TestParent.class;
Method method ;
try {
method = c.getDeclaredMethod("test", new Class[]{});
method.setAccessible(true);
try {
method.invoke(c.newInstance(), new Object[]{});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[]){
getPrivateMethod getMethod = new getPrivateMethod();
getMethod.getPriMethod();
}
}
package test优快云;
public class TestParent {
private void test(){
System.out.println("test");
}
}
访问父类的private 方法
最新推荐文章于 2023-01-07 14:39:19 发布