当前位置: 首页 > news >正文

Java高级Day41-反射入门

115.反射

反射机制

1.根据配置文件re.properties指定信息,创建Cat对象并调用hi方法

@SuppressWarnings({"all"})
public class ReflectionQuestion {public static void main(String[] args) throws IOException {//根据配置文件 re.properties 指定信息,创建Cat对象并调用方法hi//传统的方式 new 对象 -> 调用方法
//        Cat cat = new Cat();
//        cat.hi();
​//1.使用Properties类,可以肚饿写配置文件Properties properties = new Properties();properties.load(new FileInputStream("src\\re.properties"));String classfullpath = properties.get("classfullpath").toString();Object method = properties.get("method").toString();System.out.println("classfullpath=" + classfullpath);System.out.println("method=" + method);
​//2.创建对象,传统的方法行不通 -> 反射机制//new classfullpath();}
}

2.这样的需求在学习框架的时候特别多,即通过外部文件配置,在不修改源代码的情况下,来控制程序,也符合设计模式的ocp原则(开闭原则)

反射快速入门
public class ReflectionQuestion {public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {//根据配置文件 re.properties 指定信息,创建Cat对象并调用方法hi//传统的方式 new 对象 -> 调用方法
//        Cat cat = new Cat();
//        cat.hi();
​//1.使用Properties类,可以肚饿写配置文件Properties properties = new Properties();properties.load(new FileInputStream("src\\re.properties"));String classfullpath = properties.get("classfullpath").toString();String methodName = properties.get("method").toString();System.out.println("classfullpath=" + classfullpath);System.out.println("method=" + methodName);
​//2.创建对象,传统的方法行不通 -》 反射机制//new classfullpath();
​//3.使用反射机制//(1)加载类,返回Class类型的对象Class cls = Class.forName(classfullpath);//(2) 通过cls 得到你加载的类 com.reflection.question.Cat 的对象实例Object o = cls.newInstance();System.out.println("o的运行类型=" + o.getClass()); //运行类型//(3) 通过 cls 得到你加载的类 com.reflection.question.Cat 的 mothName 的方法对象//    即:在反射中,可以把方法视作对象(万物皆对象)Method method1 = cls.getMethod(methodName);//(4) 通过method1 调用方法:即通过方法对象来实现调用System.out.println("=================");method1.invoke(o);//传统方法 对象.方法() , 反射机制 方法.invok(对象)}
}
反射原理图
  1. 反射机制允许程序在执行期借助于ReflectionAPI取得任何类的内部信息(比如成员变量,构造器,成员方法等等),并能操作对象的属性及方法。反射在设计模式和框架底层都会用到

  2. 加载完类之后,在堆中就产生了一个Class类型的对象(一个类只有一个Class对象),这个对象包含了类的完整结构信息。通过这个对象得到类的结构。这个对象就像一面镜子,透过这个镜子看到类的结构,所以称之为反射

反射相关类

Java反射机制可以完成

  1. 在运行时判断任意一个对象所属的类

  2. 在运行时构造任意一个类的对象

  3. 在运行时得到任意一个类所具有的成员变量和方法

  4. 在运行时调用任意一个对象的成员变量和方法

  5. 生成动态代理

反射相关的主要类

  1. java.lang.Class:代表一个类,Class对象标识某个类加载后在堆中的对象

  2. java.lang..reflect.Method:代表类的方法,Method对象表示某个类的方法

  3. java.lang..reflect.Field:代表类的成员变量,Field对象标识某个类的成员变量

    //得到name字段
    //getField不能得到私有的属性 
    Field name = cls.getField("name");
    System.out.println(nameField.get(o));//传统写法 对象.成员变量 , 反射:成员变量对象.get(对象)  
  4. java.lang..reflect.Constructor:代表类的构造方法,Constructor表示一个构造器

    Constructor constructor = cks.getConstructor();//()中可以指定构造器参数类型,返回无参构造器
    System.out.println(constructor);//Cat()
    ​
    Constructor constructor2 = cls.getConstructor(String.class);//这里传入的String.class就是String类型的Class对象
    System.out.println(constructor2);//Cat(String name)
反射调用优化

反射优点和缺点

优点:可以动态的创建和使用对象(也是框架底层核心),使用灵活,没有反射机制,框架技术就数去底层支撑

缺点:使用反射基本是解释执行,对执行速度有影响

public class ReflectionQuestion {public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {m1();m2();}//传统方法调用hi
​public static void m1(){long start = System.currentTimeMillis();Cat cat = new Cat();for (int i = 0;i < 90000000;i++){cat.hi();}long end = System.currentTimeMillis();System.out.println("传统方法来调用hi的时间 耗时" + (end - start));//3}
​//反射机制调用方法hipublic static void m2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {Class<?> cls = Class.forName("reflection.question.Cat");Object o = cls.newInstance();Method hi = cls.getMethod("hi");long start = System.currentTimeMillis();for (int i = 0;i < 90000000;i++){hi.invoke(o);//反射调用方法}long end = System.currentTimeMillis();System.out.println("传统方法来调用hi的时间 耗时" + (end - start));//286}
}

优化-关闭访问检查

  1. Method和Field、Constructor对象都有setAccessible()方法

  2. setAccessible作用是启动和禁用访问安全检查的开关

  3. 参数值为true表示 反射的对象在使用时取消访问检查,提高反射的效率,反射值为false则表示反射的对象执行访问检查

//反射优化public static void m3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {Class cls = Class.forName("reflection.question.Cat");Object o = cls.newInstance();Method hi = cls.getMethod("hi");hi.setAccessible(true);//优化long start = System.currentTimeMillis();for (int i = 0;i < 90000000;i++){hi.invoke(o);//反射调用方法}long end = System.currentTimeMillis();System.out.println("m3方法来调用hi的时间 耗时" + (end - start));//115}
}
http://www.lryc.cn/news/438559.html

相关文章:

  • 在Linux系统上使用Docker部署java项目
  • 【C++】标准库IO查漏补缺
  • python简单易懂的lxml读取HTML节点及常用操作方法
  • Java | Leetcode Java题解之第406题根据身高重建队列
  • 安卓获取apk的公钥,用于申请app备案等
  • 【leetcode_python】杨辉三角
  • Parallels Desktop 20 for Mac中文版发布了?会哪些新功能
  • SpringBoot整合SSE-灵活管控连接
  • 挖矿木马-Linux
  • 【leetcode——415场周赛】——python前两题
  • 【CSS in Depth 2 精译_029】5.2 Grid 网格布局中的网格结构剖析(上)
  • ZYNQ LWIP(RAW API) TCP函数学习
  • Spring Boot,在应用程序启动后执行某些 SQL 语句
  • 【SQL】百题计划:SQL最基本的判断和查询。
  • 04_Python数据类型_列表
  • F5设备绑定EIP
  • 使用 PyCharm 新建 Python 项目详解
  • 从0开始学习 RocketMQ:分布式事务消息的实现
  • MySQL 查询数据库的数据总量
  • [C++]——vector
  • 自动驾驶:LQR、ILQR和DDP原理、公式推导以及代码演示(七、CILQR约束条件下的ILQR求解)
  • 随想录笔记-二叉树练习题
  • 华雁智科前端面试题
  • 【iOS】单例模式
  • Linux | 探索 Linux 信号机制:信号的产生和自定义捕捉
  • 递归的时间复杂度分析
  • C++: 二叉树进阶面试题
  • 【HarmonyOS NEXT】实现网络图片保存到手机相册
  • Pytorch详解-数据模块
  • 浅谈openresty