Java--反射--反射相关介绍

  1. java Reflection
  2. 反射原理图
  3. 反射机制可以完成什么:

  4. 反射相关的主要类:

    1. package com.model.fanshe;
      
      import java.io.FileNotFoundException;
      import java.io.FileReader;
      import java.io.IOException;
      import java.io.InputStream;
      import java.lang.reflect.Constructor;
      import java.lang.reflect.Field;
      import java.lang.reflect.InvocationTargetException;
      import java.lang.reflect.Method;
      import java.util.Properties;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/6/28 10:17
       */
      public class FanSheDemo02 {
          public static void main(String[] args) throws ClassNotFoundException, IOException, IllegalAccessException, 
                  InstantiationException, NoSuchMethodException, NoSuchFieldException, InvocationTargetException {
              String filePath="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_fanshe\\src\\main\\resources\\re.properties";
              Properties properties = new Properties();
              properties.load(new FileReader(filePath));
              String classFullPath = properties.getProperty("classFullPath");
              String methodName1 = properties.getProperty("method1");
              String methodName2 = properties.getProperty("method2");
      
      //        1.拿到类对象
              Class<?> aClass = Class.forName(classFullPath);
      //        2.拿到类的实例对象
              Object o = aClass.newInstance();
      //        3.拿到类的方法对象
              Method method1 = aClass.getMethod(methodName1);
              Method method2 = aClass.getMethod(methodName2);
      //        4.拿到类的实例变量,但是不能拿到私有的变量
              Field age = aClass.getField("age");
      //        5.拿到类的构造方法
              Constructor<?> constructor = aClass.getConstructor();//无参构造函数的对象
              Constructor<?> constructor1 = aClass.getConstructor(String.class, String.class); //拿到了无参构造函数
      //        6.调用
              Cat cat= (Cat) o;  //调用实例对象
              cat.hi();
              System.out.println(method1.invoke(o)); //调用实例方法
              System.out.println(method2.invoke(o)); //调用实例方法
              System.out.println(age.get(o)); //调用实例变量
              System.out.println(constructor); //调用无参构造函数
              System.out.println(constructor1); //调用有参构造
      
          }
      }
  5.  反射的优缺点:

    1.  

    2. 反射速度优化

      1.  

         

        放射机制调优

      2. package com.model.reflection;
        
        import java.lang.reflect.InvocationTargetException;
        import java.lang.reflect.Method;
        
        /**
         * @Description:测试类
         * @Author: 张紫韩
         * @Crete 2021/6/28 11:39
         */
        public class ReflectionDemo03 {
            public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        
                //反射机制的缺点是执行速度慢,我们可以进行调优
                m1();
                m2();
                m3();
            }
            //使用传统的方法调用方法
            public static void m1(){
                Cat cat = new Cat();
                long start = System.currentTimeMillis();
                for (int i = 0; i < 900000000; i++) {
                    cat.hi();
                }
                long end = System.currentTimeMillis();
                System.out.println("m1执行时间:"+(end-start));
        
            }
        //    使用放射机制调用了方法
            public static void m2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
                Class<?> aClass = Class.forName("com.model.reflection.Cat");
                Object o = aClass.newInstance();
                Method method = aClass.getMethod("hi");
                long start = System.currentTimeMillis();
                for (int i = 0; i < 900000000; i++) {
                    method.invoke(o);
                }
                long end = System.currentTimeMillis();
                System.out.println("m2执行时间:"+(end-start));
            }
        //    关闭反射机制调优,方法调用,关闭访问检查
            public static void m3() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
            Class<?> aClass = Class.forName("com.model.reflection.Cat");
            Object o = aClass.newInstance();
            Method method = aClass.getMethod("hi");
            method.setAccessible(true);  //关闭方法调用的检查机制
            long start = System.currentTimeMillis();
            for (int i = 0; i < 900000000; i++) {
                method.invoke(o);
            }
            long end = System.currentTimeMillis();
            System.out.println("m3执行时间:"+(end-start));
        
            }
        }

         

         

posted @ 2021-06-27 22:58  张紫韩  阅读(65)  评论(0编辑  收藏  举报