怎么获取私有的方法
private void ceshi(int a){ System.out.println("0000000000"); }
ceshi a=new ceshi(); Class c=a.getClass(); try { Method m=c.getMethod("ceshi",int.class); m.invoke(a,1); }catch (Exception e){ e.printStackTrace(); }
private void ceshi(int a){ System.out.println("0000000000"); }
ceshi a=new ceshi(); Class c=a.getClass(); try { Method m=c.getMethod("ceshi",int.class); m.invoke(a,1); }catch (Exception e){ e.printStackTrace(); }
2019-08-29
package com.herman.reflect; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class ClassDemo03 { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { A a = new A(); Class<? extends A> aClass = a.getClass(); Method self = aClass.getDeclaredMethod("self"); self.setAccessible(true); //暴力反射 self.invoke(a); } } class A { public void printf(String a) { System.out.println("printf...." + a); } private void self() { System.out.println("Access denied!"); } }
举报