• Java筑基35-反射(框架底层,必须掌握)


    目录

    一、反射机制 

    1.一个需求引出反射

    2.反射原理图

    3.反射相关类

    4. 反射调用优化

    二、Class类

    1.Class类分析

    2.Class常用方法

    3.获取class对象的各种方式(六种)

    4.哪些类型有Class对象

    三、类加载

    1.动态加载和静态加载

    2.类加载流程

    3.类加载的五个阶段

    四、反射获取类的结构信息

    1.获取类结构信息

    2.通过反射创建对象

    3.通过反射操作属性

    4.通过反射操作方法

    5.案例练习

    1)操作属性

    2)利用反射创建文件


    一、反射机制 

    1.一个需求引出反射

    re.properties

    1. classfullpath=com.feiyang.basic17_reflect.Cat
    2. --method=hi
    3. method=run

    1. package com.feiyang.basic17_reflect;
    2. /**
    3. * @author:飞扬
    4. * @公众hao:程序员飞扬
    5. * @description:
    6. */
    7. public class Cat {
    8. private String name="招财猫";
    9. public void hi(){
    10. System.out.println("hi " + name);
    11. }
    12. public void run(){
    13. System.out.println("run " + name);
    14. }
    15. }
    1. package com.feiyang.basic17_reflect;
    2. import java.io.FileInputStream;
    3. import java.io.IOException;
    4. import java.lang.reflect.InvocationTargetException;
    5. import java.lang.reflect.Method;
    6. import java.util.Properties;
    7. /**
    8. * @author:飞扬
    9. * @公众hao:程序员飞扬
    10. * @description:
    11. */
    12. public class ReflectionQuestion {
    13. public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    14. //需求:根据配置文件指定信息,创建Cat对象并调用方法
    15. //传统实现方式:
    16. /*Cat c = new Cat();
    17. c.hi();*/
    18. //明白反射
    19. //1.读去配置文件
    20. Properties properties = new Properties();
    21. properties.load(new FileInputStream("src\\re.properties"));
    22. String classfullpath = properties.get("classfullpath").toString();
    23. String method = properties.get("method").toString();
    24. System.out.println("classfullpath=" + classfullpath);
    25. System.out.println("method=" + method);
    26. //创建对象,传统方法行不通 =》 反射机制
    27. Class aClass = Class.forName(classfullpath); //获取Class对象
    28. Object o = aClass.newInstance(); //实例化对象
    29. Method method1 = aClass.getMethod(method); //通过Class对象获取方法(方法对象)
    30. method1.invoke(o); //调用方法
    31. }
    32. }

    执行结果:

    classfullpath=com.feiyang.basic17_reflect.Cat

    method=run

    run 招财猫

    2.反射原理

    反射机制原理图:

    3.反射相关类

    1. package com.feiyang.basic17_reflect;
    2. import java.io.FileInputStream;
    3. import java.io.FileNotFoundException;
    4. import java.lang.reflect.Constructor;
    5. import java.lang.reflect.Field;
    6. import java.lang.reflect.Method;
    7. import java.util.Properties;
    8. /**
    9. * @author:飞扬
    10. * @公众hao:程序员飞扬
    11. * @description:
    12. */
    13. public class Reflection01 {
    14. public static void main(String[] args) throws Exception {
    15. //1.读去配置文件
    16. Properties properties = new Properties();
    17. properties.load(new FileInputStream("src\\re.properties"));
    18. String classfullpath = properties.get("classfullpath").toString();
    19. String method = properties.get("method").toString();
    20. Class c = Class.forName(classfullpath); //获取Class对象
    21. Object o = c.newInstance(); //实例化对象
    22. //获取构造器(无参)
    23. Constructor constructor = c.getConstructor();
    24. System.out.println(constructor);
    25. //获取构造器(有参)
    26. Constructor constructor1 = c.getConstructor(String.class, int.class);
    27. System.out.println(constructor1);
    28. //获取属性
    29. Field age = c.getField("age");
    30. System.out.println(age.get(o));
    31. //获取方法
    32. Method method1 = c.getMethod(method);
    33. method1.invoke(o); //调用方法
    34. }
    35. }

    执行结果:

    public com.feiyang.basic17_reflect.Cat()

    public com.feiyang.basic17_reflect.Cat(java.lang.String,int)

    10

    run 招财猫

    4. 反射调用优化

    代码示例:

    re.properties

    1. classfullpath=com.feiyang.basic17_reflect.Cat
    2. method=hi

    1. package com.feiyang.basic17_reflect;
    2. /**
    3. * @author:飞扬
    4. * @公众hao:程序员飞扬
    5. * @description:
    6. */
    7. public class Cat {
    8. private String name="招财猫";
    9. public int age = 10;
    10. public Cat(){
    11. }
    12. public Cat(String name, int age) {
    13. this.name = name;
    14. this.age = age;
    15. }
    16. public void hi(){
    17. //System.out.println("hi " + name);
    18. }
    19. public void run(){
    20. //System.out.println("run " + name);
    21. }
    22. }

    1. package com.feiyang.basic17_reflect;
    2. import java.io.FileInputStream;
    3. import java.lang.reflect.Method;
    4. import java.util.Properties;
    5. /**
    6. * @author:飞扬
    7. * @公众hao:程序员飞扬
    8. * @description:
    9. */
    10. public class Reflection02 {
    11. public static void main(String[] args) throws Exception {
    12. m1();
    13. m2();
    14. m3();
    15. }
    16. //传统方法调用
    17. public static void m1(){
    18. Cat cat = new Cat();
    19. long l = System.currentTimeMillis();
    20. for (int i = 0; i < 900000000; i++) {
    21. cat.hi();
    22. }
    23. long l1 = System.currentTimeMillis();
    24. System.out.println(l1-l);
    25. }
    26. //反射方式调用
    27. public static void m2() throws Exception {
    28. //1.读去配置文件
    29. Properties properties = new Properties();
    30. properties.load(new FileInputStream("src\\re.properties"));
    31. String classfullpath = properties.get("classfullpath").toString();
    32. String method = properties.get("method").toString();
    33. Class c = Class.forName(classfullpath); //获取Class对象
    34. Object o = c.newInstance(); //实例化对象
    35. //获取方法
    36. Method method1 = c.getMethod(method);
    37. long l = System.currentTimeMillis();
    38. for (int i = 0; i < 900000000; i++) {
    39. method1.invoke(o); //调用方法
    40. }
    41. long l1 = System.currentTimeMillis();
    42. System.out.println(l1-l);
    43. }
    44. //反射调用优化(禁止安全检查)
    45. public static void m3() throws Exception {
    46. //1.读去配置文件
    47. Properties properties = new Properties();
    48. properties.load(new FileInputStream("src\\re.properties"));
    49. String classfullpath = properties.get("classfullpath").toString();
    50. String method = properties.get("method").toString();
    51. Class c = Class.forName(classfullpath); //获取Class对象
    52. Object o = c.newInstance(); //实例化对象
    53. //获取方法
    54. Method method1 = c.getMethod(method);
    55. method1.setAccessible(false);
    56. long l = System.currentTimeMillis();
    57. for (int i = 0; i < 900000000; i++) {
    58. method1.invoke(o); //调用方法
    59. }
    60. long l1 = System.currentTimeMillis();
    61. System.out.println(l1-l);
    62. }
    63. }

    Relection02执行结果:

    0

    1954

    1838

    二、Class类

    1.Class类分析

    先看下Class类的类图:

    可以看到Class类和别的类是一样的,同样继承了Object类,只是他的使用方法和所完成的功能有些特殊性,仅此而已(所以啦,不要觉得他很神秘呦)

    示例:

    1. package com.feiyang.basic17_reflect;
    2. /**
    3. * @author:飞扬
    4. * @公众hao:程序员飞扬
    5. * @description: Class类分析
    6. */
    7. public class Class01 {
    8. public static void main(String[] args) throws ClassNotFoundException {
    9. //传统方法
    10. /**
    11. * debug发现:
    12. *
    13. * ClassLoader类的方法,加载
    14. * public Class loadClass(String name) throws ClassNotFoundException {
    15. * return loadClass(name, false);
    16. *
  • 相关阅读:
    【UNIX网络编程】| 【05】套接字选项(详)
    opensl学习——base16编码解码、base64编码解码、ASCII码表、扩展ASCII码
    实现微信机器人开发,个微api
    浅析能耗管理系统在企业中的应用
    【安全框架】快速了解安全框架
    错排问题 (抽奖,发邮件)
    PV & PVC in K8s
    如何在Win11下安装linux子系统(WSL1),并配置anaconda+pytorch深度学习环境的完整教程(30系列显卡包括RTX3090也适用)
    软件测试/测试开发/人工智能丨Python类型转换
    分布式协调服务
  • 原文地址:https://blog.csdn.net/lu_xin5056/article/details/127135671