【问题描述】通过 Java 反射访问类里面的私有变量和方法 【实例】 package com.jack.zhang.reflect; import java.lang.reflect.Field; /** * @author Jack Zhang * @version vb1.0 * @Email virgoboy2004@163.com * @Date 2011-6-26 */ public class ReflectTest { public static void main(String[] args) throws Exception { ClassLoader loader = Thread.currentThread().getContextClassLoader(); Class clazz = loader.loadClass("com.jack.zhang.reflect.Target"); Target rTarget = (Target) clazz.newInstance(); Field field = clazz.getDeclaredField("i"); field.setAccessible(true); field.set(rTarget, 100); System.out.println(rTarget.getI());//输出为 100 } } class Target { private int i = 0; public int getI() { return i; } }