java 反射获取private例子

  1. public static void main(String[] args) {
  2. String objName="com.che.test.User";
  3. try{
  4. Class cs=Class.forName(objName);
  5. Object obj=cs.newInstance();
  6. Field name=cs.getDeclaredField("name");
  7. name.setAccessible(true);
  8. System.out.println(name.get(obj));
  9. name.set(obj, "che");
  10. System.out.println(name.get(obj));
  11. name.setAccessible(false);
  12. }catch(Exception e){
  13. e.printStackTrace();
  14. }
  15. }

你可能感兴趣的:(private)