普通开发模式下的代码是编译时已经设置好参数和属性, 反射是在运行时候动态注入参数属性

image.png

Class 类

  • 在jvm 中代表 类和接口 的类
  • Class 对象具体包含了某个特定类的结构信息
  • 通过Class 对象可以获取到类的构造方法、方法、成员变量等
  • image.png ```java public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

    1. //ClassNotFoundException 类找不到
    2. //InstantiationException 非法访问异常
    3. //IllegalAccessException 无法实例化异常
    4. // 指定的类加载到jvm中, 返回对应的class 对象
    5. Class employeeClass = Class.forName("cn.ichief.reflect.entity.Employee");
    6. // 通过默认构造方法实例对象 默认使用无参数的 newInstance 返回的是Object 需要强制转换
    7. Employee employee = (Employee) employeeClass.newInstance();
  1. }
  1. <a name="FANjH"></a>
  2. #### Constructor 构造方法类
  3. - ![image.png](https://cdn.nlark.com/yuque/0/2021/png/422907/1639283946710-6b7303a0-b444-45cf-b09b-a70afd8652e9.png#clientId=ud9167f32-073b-4&from=paste&height=316&id=uec2d8a47&margin=%5Bobject%20Object%5D&name=image.png&originHeight=632&originWidth=2586&originalType=binary&ratio=1&size=591295&status=done&style=none&taskId=u3735c32c-26e6-4b29-8204-f496dd79d52&width=1293)
  4. ```java
  5. try {
  6. Class EmployeeClass = Class.forName("cn.ichief.reflect.entity.Employee");
  7. // 对应构造方法的参数类型
  8. Constructor constructor = EmployeeClass.getConstructor(new Class[]{
  9. Integer.class,
  10. String.class,
  11. Float.class,
  12. String.class
  13. });
  14. Employee employee = (Employee) constructor.newInstance(new Object[]{
  15. 100, "ss", 3000f, "yfb"
  16. });
  17. } catch (ClassNotFoundException e) {
  18. e.printStackTrace();
  19. } catch (NoSuchMethodException e) {
  20. e.printStackTrace();
  21. } catch (InvocationTargetException e) {
  22. e.printStackTrace();
  23. } catch (InstantiationException e) {
  24. e.printStackTrace();
  25. } catch (IllegalAccessException e) {
  26. e.printStackTrace();
  27. }

Method 方法类

  • image.png ```java public static void main(String[] args) {

    1. try {
    2. Class EmployeeClass = Class.forName("cn.ichief.reflect.entity.Employee");
    3. // 对应构造方法的参数类型
    4. Constructor constructor = EmployeeClass.getConstructor(new Class[]{
    5. Integer.class,
    6. String.class,
    7. Float.class,
    8. String.class
    9. });
    10. Employee employee = (Employee) constructor.newInstance(new Object[]{
    11. 100, "ss", 3000f, "yfb"
    12. });
  1. Method updateSalaryMethod = EmployeeClass.getMethod("updateSalary", Float.class);
  2. Employee employee1 = (Employee) updateSalaryMethod.invoke(employee, 100f);
  3. } catch (ClassNotFoundException e) {
  4. e.printStackTrace();
  5. } catch (NoSuchMethodException e) {
  6. e.printStackTrace();
  7. } catch (InvocationTargetException e) {
  8. e.printStackTrace();
  9. } catch (InstantiationException e) {
  10. e.printStackTrace();
  11. } catch (IllegalAccessException e) {
  12. e.printStackTrace();
  13. }
  14. }
  1. <a name="l2DTV"></a>
  2. #### Field 成员变量类
  3. - ![image.png](https://cdn.nlark.com/yuque/0/2021/png/422907/1639284803877-2be23cc2-f17a-4adb-b53c-aa8c6594c297.png#clientId=ud9167f32-073b-4&from=paste&height=642&id=ud0380cd4&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1284&originWidth=2416&originalType=binary&ratio=1&size=634266&status=done&style=none&taskId=ua885e5b5-83ee-4ff2-9fa4-f5eb5da12c5&width=1208)
  4. - ![image.png](https://cdn.nlark.com/yuque/0/2021/png/422907/1639284816663-be036d98-94f4-4d60-ad1c-1171e6979390.png#clientId=ud9167f32-073b-4&from=paste&height=437&id=u6cceaaad&margin=%5Bobject%20Object%5D&name=image.png&originHeight=874&originWidth=2702&originalType=binary&ratio=1&size=735030&status=done&style=none&taskId=u0c1031ca-b2c8-4696-a26c-8086e507cc8&width=1351)
  5. ```java
  6. public static void main(String[] args) {
  7. try {
  8. Class EmployeeClass = Class.forName("cn.ichief.reflect.entity.Employee");
  9. // 对应构造方法的参数类型
  10. Constructor constructor = EmployeeClass.getConstructor(new Class[]{
  11. Integer.class,
  12. String.class,
  13. Float.class,
  14. String.class
  15. });
  16. Employee employee = (Employee) constructor.newInstance(new Object[]{
  17. 100, "ss", 3000f, "yfb"
  18. });
  19. Field enameField = EmployeeClass.getField("ename");
  20. enameField.set(employee, "ssssd");
  21. String ename = enameField.get(employee).toString();
  22. } catch (ClassNotFoundException e) {
  23. e.printStackTrace();
  24. } catch (NoSuchMethodException e) {
  25. e.printStackTrace();
  26. } catch (InvocationTargetException e) {
  27. e.printStackTrace();
  28. } catch (InstantiationException e) {
  29. e.printStackTrace();
  30. } catch (IllegalAccessException e) {
  31. e.printStackTrace();
  32. } catch (NoSuchFieldException e) {
  33. e.printStackTrace();
  34. }
  35. }

getConstructor 只能获取public private 的使用 getDeclaredConstructor

image.png