当前位置: 首页> 科技> 数码 > 网页图片不能保存怎么办_公司网站_登封seo公司_网络营销的概念及内容

网页图片不能保存怎么办_公司网站_登封seo公司_网络营销的概念及内容

时间:2025/8/15 8:34:33来源:https://blog.csdn.net/2401_87473474/article/details/147395732 浏览次数:2次
网页图片不能保存怎么办_公司网站_登封seo公司_网络营销的概念及内容

反射能干的事情包括:

允许程序在运行时,动态地获取类的信息、创建对象、调用方法以及访问和修改对象的字段
1.创建类/接口的对应class对象
2.进而获取并操作该对象所属类的成员(成员变量,成员方法,构造方法)

演示:

public class Cellphone{private String model;// 静态代码块类加载时执行,只执行一次static {System.out.println("Cellphone的静态代码块");}public Cellphone() {}public Cellphone(String model){this.model = model;}public String getModel() {return model;}public void setModel(String model) {this.model = model;}public void call(long number){System.out.println("正在呼叫"+number);}}
@Testpublic void testReflectSetFiled() {// 1.获取 class  三种方法,根据情况选择01:Class<Cellphone> cellphoneClass = Cellphone.class;02:Class<? extends Cellphone> cellphoneClass = cellphone.getClass();继承03:Class<?> cellphoneClass Class.forName("com.basic.foundational.lang.Cellphone");已知类全路径 try { // 2.获取并操作构造方法 Constructor<Cellphone> constructor = cellphoneClass.getDeclaredConstructor();System.out.println("获取无参构造方法: "+constructor);Constructor<Cellphone> StringConstructor =   cellphoneClass.getDeclaredConstructor(String.class);System.out.println("获取有参构造方法: "+StringConstructor);Constructor<?>[] allConstructors = cellphoneClass.getDeclaredConstructors();for (Constructor<?> allConstructor : allConstructors) {System.out.println("获取所有构造方法 "+allConstructor);}Constructor<Cellphone> privateConstructor = cellphoneClass.getDeclaredConstructor();//暴力反射 使用权限检查 private构造方法也能调用 privateConstructor.setAccessible(true);// 3.使用反射机制创建 cellphone 对象Cellphone cellphone = privateConstructor.newInstance();System.out.println(cellphone);// 4.获取,操作成员变量Field[] allFileds = cellphoneClass.getDeclaredFields();for (Field allFiled : allFileds) {System.out.println("所有的成员变量是: "+ allFiled);}// 默认为false 开启后可以拿到private修饰的成员变量modelFiled.setAccessible(true);Field modelFiled = cellphoneClass.getDeclaredField("model");System.out.println("modelFiled表示的成员变量是: "+modelFiled.getName());// 操作成员变量,设置属性值,获取属性值modelFiled.set(cellphone,"iphone11");System.out.println("cellphone的型号是: "+modelFiled.get(cellphone));// 5.获取并操作成员方法Method[] allMethods = cellphoneClass.getDeclaredMethods();for (Method allMethod : allMethods) {System.out.println("Cellphone所有方法:"+allMethod);}Method callMethod = cellphoneClass.getDeclaredMethod("call", long.class);System.out.println("Cellphone的call方法的信息是: "+callMethod);//调用callMethod.invoke(cellphone,123456789L);}catch (Exception e) {e.printStackTrace();}}

反射,在平时的程序里很少直接使用,反射一般是用在框架的底层实现里会用到,这些框架通过反射调用类的构造函数完成对象的创建,并进行属性的设置和获取。

比如:

最典型的就是Spring 框架的依赖注入(DI),在 Spring 容器启动时,会依据配置文件或者注解信息,借助反射创建对象实例,并且将对象的依赖关系注入到相应的类中。

AOP(面向切面编程)通过反射获取目标对象的方法信息,然后在方法调用前后插入相应的增强逻辑,比如日志记录、事务管理等。

Hibernate 框架,作为一个 ORM 框架,通过反射获取类的属性和方法信息,然后依据映射配置文件或者注解,将对象的属性值映射到数据库表的字段中,或者从数据库中查询数据并映射到 Java 对象中。

关键字:网页图片不能保存怎么办_公司网站_登封seo公司_网络营销的概念及内容

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: