java 反照获取private例子
java 反射获取private例子public static void main(String[] args) { String objNamecom.che.test.User
java 反射获取private例子
public static void main(String[] args) { String objName="com.che.test.User"; - try{ Class cs=Class.forName(objName);
- Object obj=cs.newInstance(); Field name=cs.getDeclaredField("name");
- name.setAccessible(true); System.out.println(name.get(obj));
- name.set(obj, "che"); System.out.println(name.get(obj));
- name.setAccessible(false); }catch(Exception e){
- e.printStackTrace(); }
- }