1. package com.chao.reflection;
  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. //动态的创建对象,通过反射
  7. public class Test09 {
  8. public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
  9. //获得Class对象
  10. Class c1 = Class.forName("com.chao.reflection.User");
  11. //构造一个对象
  12. //User user = (User)c1.newInstance(); //本质是调用了类的无参构造器
  13. //System.out.println(user);
  14. //通过构造器创建对象
  15. //Constructor constructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
  16. //User user2 = (User)constructor.newInstance("追梦王子", 001, 18);
  17. //System.out.println(user2);
  18. //通过反射调用普通方法
  19. User user3 = (User)c1.newInstance();
  20. //通过反射获取一个方法
  21. Method setName = c1.getDeclaredMethod("setName", String.class);
  22. //invoke : 激活的意思
  23. //(对象,"方法的值")
  24. setName.invoke(user3,"追梦王子");
  25. System.out.println(user3.getName());
  26. //通过反射操作属性
  27. System.out.println("8888888888888888888888888888888888");
  28. User user4 = (User)c1.newInstance();
  29. Field name = c1.getDeclaredField("name");
  30. //不能直接操作私有属性,我们需要关闭程序的安全检测,属性或者方法的setAccessible(true).
  31. name.setAccessible(true);
  32. name.set(user4,"追梦王子2");
  33. System.out.println(user4.getName());
  34. }
  35. }
  1. package com.chao.reflection;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. //分析性能问题
  5. public class Test10 {
  6. //普通方式调用
  7. public static void test01(){
  8. User user = new User();
  9. long startTime = System.currentTimeMillis();
  10. for (int i = 0; i < 1000000000; i++) {
  11. user.getName();
  12. }
  13. long endTime = System.currentTimeMillis();
  14. System.out.println("普通方式执行10亿次:"+(endTime-startTime)+"ms");
  15. }
  16. //反射方式调用
  17. public static void test02() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
  18. User user = new User();
  19. Class c1 = user.getClass();
  20. Method getName = c1.getDeclaredMethod("getName", null);
  21. long startTime = System.currentTimeMillis();
  22. for (int i = 0; i < 1000000000; i++) {
  23. getName.invoke(user,null);
  24. }
  25. long endTime = System.currentTimeMillis();
  26. System.out.println("反射方式执行10亿次:"+(endTime-startTime)+"ms");
  27. }
  28. //反射方式调用 关闭检测
  29. public static void test03() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
  30. User user = new User();
  31. Class c1 = user.getClass();
  32. Method getName = c1.getDeclaredMethod("getName", null);
  33. getName.setAccessible(true);
  34. long startTime = System.currentTimeMillis();
  35. for (int i = 0; i < 1000000000; i++) {
  36. getName.invoke(user,null);
  37. }
  38. long endTime = System.currentTimeMillis();
  39. System.out.println("关闭检测执行10亿次:"+(endTime-startTime)+"ms");
  40. }
  41. public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
  42. test01();
  43. test02();
  44. test03();
  45. }
  46. }

版权声明:本文为Answer-Chao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Answer-Chao/p/14664472.html