用反射访问私有成员变量
用JAVA反射机制访问私有成员变量
1. PrivateTester.java
package com.test;public class PrivateTester {private String name="hello world";public String getName(){return this.name;}}package com.test;import java.lang.reflect.Field;public class ReflectionTest {/** * @param args * @throws Exception * @throws */public static void main(String[] args) throws Exception {PrivateTester pt=new PrivateTester();Class<?> classType=PrivateTester.class;Field field=classType.getDeclaredField("name");field.setAccessible(true); //压制JAVA访问控制检查String str=(String)field.get(pt);System.out.println(str);field.set(pt, "world");System.out.println(field.get(pt));}}