访问父类的private 方法

来源:互联网 发布:scala编程pdf完整 编辑:程序博客网 时间:2024/04/29 05:58
package testCSDN;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 blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String args[]){getPrivateMethod getMethod = new getPrivateMethod();getMethod.getPriMethod();}}package testCSDN;public class TestParent {private void test(){System.out.println("test");}}
原创粉丝点击