利用反射调用私有方法
2017-02-13 20:01
183 查看
Teacher类中的私有方法:
调用代码:
NOTE:
private void printToString() { System.out.println(toString()); }
调用代码:
try { Teacher t = new Teacher(); Method method = Teacher.class.getDeclaredMethod("printToString"); method.setAccessible(true); method.invoke(t); } catch (Exception e) { e.printStackTrace(); }
NOTE:
method.setAccessible(true);必须写在
method.invoke(t);之前。
相关文章推荐
- java利用反射来调用一个类的私有方法
- 利用反射对私有属性/方法进行设置/调用(转自己贴)
- Java记录 -88- 利用反射机制调用对象的私有方法和属性
- Java反射高级应用--利用反射调用类的私有方法修改私有方法值,以及替换Java的类成员数据
- java利用反射来调用一个类的私有方法和成员变量
- 利用java的反射机制调用类的私有方法及私有属性
- Java记录 -88- 利用反射机制调用对象的私有方法和属性
- Java反射高级应用--利用反射调用类的私有方法修改私有方法值,以及替换Java的类成员数据
- 利用反射调用私有方法、访问私有属性
- 利用反射对私有属性/方法进行设置/调用
- Java反射高级应用--利用反射调用类的私有方法修改私有方法值,以及替换Java的类成员数据
- C#利用反射调用基类私有方法 及 Unity实现自定义InputField
- 利用反射访问私有属性、调用私有方法
- 利用java反射调用类的的私有方法
- Reflection——利用反射调用构造函数和私有字段
- C#反射调用另外一个类中的私有字段和方法
- 利用反射机制动态调用方法
- 如何防止私有方法被反射调用?
- 利用虚拟方法和反射简化Alisoft API的调用
- 利用java反射机制调用类的私有方法