反射及动态代理

反射

        定义:

                反射允许对封装类的字段,方法和构造 函数的信息进行编程访问

                

                                                                        图来自黑马程序员 

        获取class对象的三种方式:

                1)Class.forName("全类名")

                2)类名.class

                3) 对象.getClass()

                

                                                                         图来自黑马程序员  

                

package com.lazyGirl.reflectdemo;public class MyReflectDemo1 {public static void main(String[] args) throws ClassNotFoundException {//最常见Class clazz = Class.forName("com.lazyGirl.reflectdemo.Student");System.out.println(clazz);Class clazz2 = Student.class;System.out.println(clazz2);System.out.println(clazz == clazz2);Student student = new Student();Class clazz3 = student.getClass();System.out.println(clazz3);System.out.println(clazz == clazz3);}
}

        输出:

         

 获取构造方法:

                                                                     图来自黑马程序员  

package com.lazyGirl.reflectdemo.demo1;import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Parameter;public class Demo1 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {Class clazz = Class.forName("com.lazyGirl.reflectdemo.demo1.Student");Constructor[] cons = clazz.getConstructors();for (Constructor c : cons) {System.out.println(c);}System.out.println();Constructor[] cons1 = clazz.getDeclaredConstructors();for (Constructor c : cons1) {System.out.println(c);}System.out.println();Constructor cons2 = clazz.getDeclaredConstructor();System.out.println(cons2);System.out.println();Constructor cons3 = clazz.getDeclaredConstructor(String.class);System.out.println(cons3);System.out.println();Constructor cons4 = clazz.getDeclaredConstructor(int.class);System.out.println(cons4);System.out.println();Constructor cons5 = clazz.getDeclaredConstructor(String.class, int.class);System.out.println(cons5);int modifiers = cons5.getModifiers();System.out.println(modifiers);System.out.println();Parameter[] parameters = cons5.getParameters();for (Parameter p : parameters) {System.out.println(p);}cons5.setAccessible(true);Student stu = (Student) cons5.newInstance("hhh",16);System.out.println(stu);}
}

        输出:

         

 获取成员变量:

                                                                 图来自黑马程序员  

package com.lazyGirl.reflectdemo.demo2;import java.lang.reflect.Field;public class Demo {public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {Class clazz = Class.forName("com.lazyGirl.reflectdemo.demo2.Student");Field[] fields = clazz.getFields();for (Field field : fields) {System.out.println(field.getName());}System.out.println();Field[] declaredFields = clazz.getDeclaredFields();for (Field field : declaredFields) {System.out.println(field.getName());}System.out.println();Field gender = clazz.getField("gender");System.out.println(gender);System.out.println();Field declaredName = clazz.getDeclaredField("name");System.out.println(declaredName);System.out.println();int modifiers = declaredName.getModifiers();System.out.println(modifiers);String name = declaredName.getName();System.out.println(name);System.out.println();Class<?> type = declaredName.getType();System.out.println(type);Student student = new Student("hhh",16,"女");declaredName.setAccessible(true);Object value = declaredName.get(student);System.out.println(value);declaredName.set(student,"hhhh");System.out.println(student);}
}

输出:

获取成员方法:

                                                         图来自黑马程序员 

package com.lazyGirl.reflectdemo.demo3;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;public class Demo {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {Class clazz = Class.forName("com.lazyGirl.reflectdemo.demo3.Student");//包含父类的方法
//        Method[] methods = clazz.getMethods();//不能获取父类方法,但是可以访问私有方法Method[] methods = clazz.getDeclaredMethods();for (Method method : methods) {System.out.println(method);}Method method = clazz.getMethod("eat", String.class);System.out.println(method);int modifiers = method.getModifiers();System.out.println(modifiers);String name = method.getName();System.out.println(name);Parameter[] parameters = method.getParameters();for (Parameter parameter : parameters) {System.out.println(parameter);}Class[] exceptions = method.getExceptionTypes();for (Class exception : exceptions) {System.out.println(exception);}Student student = new Student();method.invoke(student,"cake");}
}

Student类:

package com.lazyGirl.reflectdemo.demo3;public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void sleep(){System.out.println("sleep");}public void eat(String food) throws InterruptedException,ClassCastException{System.out.println("eat " + food);}public void eat(String food, int time){System.out.println("eat " + food + " " + time);}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

输出:

作用:

        获取一个类里面所有的信息,获取到了之后,再执行其他业务逻辑

        结合配置文件,动态的创建对象并调用方法

        

package com.lazyGirl.reflectdemo.demo5;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;public class Test {public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {Properties prop = new Properties();FileInputStream fis = new FileInputStream("pro.properties");prop.load(fis);fis.close();System.out.println(prop);String classname = (String) prop.get("classname");String methdName = (String) prop.get("method");System.out.println(classname);System.out.println(methdName);Class clazz = Class.forName(classname);Constructor constructor = clazz.getConstructor();Object o = constructor.newInstance();System.out.println(o);Method method = clazz.getDeclaredMethod(methdName);method.setAccessible(true);method.invoke(o);}
}

        properties文件:

classname=com.lazyGirl.reflectdemo.demo5.Student
method=study

        输出:

 

 动态代理:

        无侵入式的给代码增加额外的功能

        程序为什么需要代理:对象身上干的事太多,可以通过代理来转移部分职业

        对象有什么方法想被代理,代理就一定要有对应的方法

        java通过接口保证对象和代理

        格式:

        

                        图来自黑马程序员

                

        

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/34395.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

pytest-yaml-sanmu(五):跳过执行和预期失败

除了手动注册标记之外&#xff0c;pytest 还内置了一些标记可直接使用&#xff0c;每种内置标记都会用例带来不同的特殊效果&#xff0c;本文先介绍 3 种。 1. skip skip 标记通常用于忽略暂时无法执行&#xff0c;或不需要执行的用例。 pytest 在执行用例时&#xff0c;如果…

Nuxt框架 和 Vite框架比较

共同点 基于 Vue.js&#xff1a;Nuxt 和 Vite 都是围绕 Vue.js 构建的&#xff0c;这意味着它们可以利用 Vue.js 的响应式数据绑定和组件系统。 现代前端开发&#xff1a;两者都支持现代前端开发实践&#xff0c;如组件化、模块化和单文件组件&#xff08;SFCs&#xff09;。 V…

提升你的创造力的几个重要技巧

保持输入输出&#xff0c;光出水而不补充的水缸就会空&#xff0c;人要时刻保持学习的习惯&#xff1b; 深刻意识到信息即反应&#xff0c;主动屏蔽噪声和垃圾人的打扰的能力决定你是否拥有时间&#xff1b; 事有轻重缓急的分类&#xff0c;优先选择那些具有挑战性的任务&#…

LATR 算法解读

文章目录 1. 论文2. 环境安装3. 代码解读3. 1 初始化 lane query3.1.1 SparseInsDecoder3.1.2 loss 计算3.1.3 初始化instance query3.2 ref points 的生成3.3 lane query 和feats进行attention3.3.1 self attn3.3.1 cross attn4. 参考1. 论文 2. 环境安装 146 [2024-06-20 10…

收款机TTS语音芯片新方案:WT3000T8,双语合成流畅,字库解码多样!

发布时间&#xff1a;2024-06-26 09:20 浏览次数&#xff1a;88次 一&#xff1a;方案背景概述 随着科技的飞速发展&#xff0c;人工智能和语音识别技术在各个领域都得到了广泛应用。其中&#xff0c;文本转语音&#xff08;TTS&#xff09;技术以其独特的优势&#xff0c;在收…

【C++/STL】:list容器的深度剖析及模拟实现

目录 &#x1f680;前言&#x1f680;一&#xff0c;节点类&#x1f680;二&#xff0c;迭代器类1&#xff0c;普通迭代器类的实现2&#xff0c;->运算符的使用场景3&#xff0c;const迭代器类的实现4&#xff0c;通过模板参数&#xff0c;把两个类型的迭代器类结合5&#x…

基于springboot+vue的梦幻玩具乐园的设计与实现(在线购物平台)

需要源码和论文的小伙伴可以私信博主&#xff08;有偿&#xff09; ​​​​​课题目的与意义 随着互联网的不断普及与在线销售平台的迅猛发展&#xff0c;在线购物日益受到广大消费者的青睐与追捧。通过构建基于Spring BootVue的在线玩具商城&#xff0c;可以为玩具制造商、…

如何快速交付网络基础设施运维管理软件项目?

​ 基于nVisual网络基础设施数字孪生管理工具 开发项目需求 项目交付成本节省50%、进度提高100% ​ &#xff1e;&#xff1e;&#xff1e;nVisual主要功能&#xff1c;&#xff1c;&#xff1c; 01 场 景 ★ 支持层次化的场景结构 ★ 支持多种空间场景 ​ 02 规 划 ★ 丰…

基于Pytorch框架的深度学习ConvNext神经网络宠物猫识别分类系统源码

第一步&#xff1a;准备数据 12种宠物猫类数据&#xff1a;self.class_indict ["阿比西尼猫", "豹猫", "伯曼猫", "孟买猫", "英国短毛猫", "埃及猫", "缅因猫", "波斯猫", "布偶猫&q…

Go语言之函数和方法

个人网站&#xff1a; http://hardyfish.top/ 免费书籍分享&#xff1a; 资料链接&#xff1a;https://url81.ctfile.com/d/57345181-61545511-81795b?p3899 访问密码&#xff1a;3899 免费专栏分享&#xff1a; 资料链接&#xff1a;https://url81.ctfile.com/d/57345181-6…

学习TS看这一篇就够了!

目录 TS的优点和缺点基础类型数字类型布尔类型字符串类型void 类型null 类型和 undefined 类型bigint类型Symbol类型 其他类型数组元组枚举Enum对象和函数any void never unknown 的区别是什么泛型 Generic交叉类型联合类型 特殊符号 ? ?. ?? ! _修饰符 TS的优点和缺点 优…

GPT对话代码库——STM32G431微秒(us)级delay函数

目录 1&#xff0c;问&#xff1a; 1&#xff0c;答&#xff1a; 方法一&#xff1a;使用定时器&#xff08;Timer&#xff09; 方法二&#xff1a;使用SysTick定时器 方法三&#xff1a;使用内联汇编 选择合适的方法 2&#xff0c;问&#xff1a; 2&#xff0c;答&…

如何集成CppCheck到visual studio中

1.CPPCheck安装 在Cppcheck官方网站下载最新版本1.70&#xff0c;官网链接&#xff1a;http://cppcheck.sourceforge.net/ 安装Cppcheck 2.集成步骤 打开VS&#xff0c;菜单栏工具->外部工具->添加&#xff0c;按照下图设置&#xff0c;记得勾选“使用输出窗口” 2.…

深入解析 IPython 命名空间与作用域机制

IPython 是一个强大的交互式 Python 解释器&#xff0c;它提供了许多增强的功能来改善用户的编程体验。在 IPython 中&#xff0c;命名空间&#xff08;namespace&#xff09;和作用域&#xff08;scope&#xff09;的概念对于理解变量的生命周期和访问方式至关重要。本文将详细…

word2016中新建页面显示出来的页面没有页眉页脚,只显示正文部分。解决办法

问题描述&#xff1a;word2016中新建页面显示出来的页面没有页眉页脚&#xff0c;只显示正文部分。设置了页边距也不管用。 如图1 图1 解决&#xff1a; 点击“视图”——“多页”——“单页”&#xff0c;即可。如图2操作 图2 结果展示&#xff1a;如图3 图3

【Unity】数据持久化--JSON

1、JSON基础语法 1.1 注释内容 单行注释 // 多行注释 /* 内容 */ //注释内容 /* 多行注释 123 e1 ds */ /* 1.2 符号含义 大括号 {} 对象 中括号 [] 数组 冒号 : 键值对对应关系 逗号 , 数据分割 双引号 "" 键名/字符串 1.3 键值对表示 “ "键…

AI 大模型之美 | 更新完结

AI 大模型&#xff1a;技术的壮丽与美感 在当今的人工智能领域&#xff0c;大模型如同一座座巨大的桥梁&#xff0c;将计算机科学、语言学、认知科学等多个领域连接在一起。它们不仅仅是技术的象征&#xff0c;更是人类智慧与创新的结晶。本文将探讨AI大模型的壮丽与美感&…

基于Java的订餐小程序【附源码】

一、本选题的依据&#xff08;阐述所选课题的研究背景、研究目的和意义、分析国内外研究现状及趋势&#xff09; 研究背景&#xff1a; 随着移动互联网的普及和智能手机的发展&#xff0c;人们的生活方式正在发生深刻的变化。特别是在餐饮行业&#xff0c;传统的堂食模式已不能…

‘pip‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。

&#x1f4da;博客主页&#xff1a;knighthood2001 ✨公众号&#xff1a;认知up吧 &#xff08;目前正在带领大家一起提升认知&#xff0c;感兴趣可以来围观一下&#xff09; &#x1f383;知识星球&#xff1a;【认知up吧|成长|副业】介绍 ❤️如遇文章付费&#xff0c;可先看…

【深度学习】快速入门KerasNLP:微调BERT模型完成电影评论情感分类任务

简介&#xff1a;本文将介绍 KerasNLP 的安装及使用&#xff0c;以及如何使用它在情感分析任务中微调 BERT 的预训练模型。 1. KerasNLP库 KerasNLP 是一个自然语言处理库&#xff0c;兼容 TensorFlow、JAX 和 PyTorch 等多种深度学习框架。基于 Keras 3 构建&#xff0c;这些…