糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > 反射反射反射

反射反射反射

时间:2019-09-27 06:06:58

相关推荐

反射反射反射

反射

1.获取类对象2.获取成员变量3.获取成员方法4.获取构造器5.哪些类型有 Class 对象

首先我们了解一下JVM,什么是JVM,Java的虚拟机,java之所以能跨平台就是因为这个东西,你可以理解成一个进程,程序,只不过他的作用是用来跑你的代码的。上图是java的内存模型,我们关注的点,一个方法区,一个栈,一个堆,初学的时候老师不深入的话只告诉你java的内存分为堆和栈,易懂点吧!假如你写了一段代码:Object o=new Object();运行了起来!首先JVM会启动,你的代码会编译成一个.class文件,然后被类加载器加载进jvm的内存中,你的类Object加载到方法区中,创建了Object类的class对象到堆中,注意这个不是new出来的对象,而是类的类型对象,每个类只有一个class对象,作为方法区类的数据结构的接口。jvm创建对象前,会先检查类是否加载,寻找类对应的class对象,若加载好,则为你的对象分配内存,初始化也就是代码:new Object()。上面的流程就是你自己写好的代码扔给jvm去跑,跑完就over了,jvm关闭,你的程序也停止了。为什么要讲这个呢?因为要理解反射必须知道它在什么场景下使用。题主想想上面的程序对象是自己new的,程序相当于写死了给jvm去跑。假如一个服务器上突然遇到某个请求哦要用到某个类,哎呀但没加载进jvm,是不是要停下来自己写段代码,new一下,哦启动一下服务器,(脑残)! 反射是什么呢?当我们的程序在运行时,需要动态的加载一些类这些类可能之前用不到所以不用加载到jvm,而是在运行时根据需要才加载,这样的好处对于服务器来说不言而喻,举个例子我们的项目底层有时是用mysql,有时用oracle,需要动态地根据实际情况加载驱动类,这个时候反射就有用了,假设 com.java.dbtest.myqlConnection,com.java.dbtest.oracleConnection这两个类我们要用,这时候我们的程序就写得比较动态化,通过Class tc =Class.forName(“com.java.dbtest.TestConnection”);通过类的全类名让jvm在服务器中找到并加载这个类,而如果是oracle则传入的参数就变成另一个了。这时候就可以看到反射的好处了,这个动态性就体现出java的特性了!举多个例子,大家如果接触过spring,会发现当你配置各种各样的bean时,是以配置文件的形式配置的,你需要用到哪些bean就配哪些,spring容器就会根据你的需求去动态加载,你的程序就能健壮地运行

1.获取类对象

//1.将字节码文件加载进内存获取Class类对象(源代码阶段)Class<?> aClass = Class.forName("com.example.nacosCofigClient.JDK8.Reflection.RePerson");System.out.println(aClass);//class com.example.demo.JDK8.Reflection.RePerson//2.Class类对象阶段Class<RePerson> rePersonClass = RePerson.class;//class com.example.demo.JDK8.Reflection.RePersonSystem.out.println(rePersonClass);//3.对象.getClassRePerson rePerson = new RePerson();Class<? extends RePerson> personClass = rePerson.getClass();System.out.println(personClass);//class com.example.demo.JDK8.Reflection.RePerson/*** 4.比较对象的内存地址(都是一样的)*/System.out.println(aClass == rePersonClass);//trueSystem.out.println(rePersonClass == personClass);//true

2.获取成员变量

首先建一个类

public class RePerson {public Integer id;public String name;public Boolean flag;private Double price;

获取成员变量数组

//获取person的class对象Class<RePerson> rePersonClass = RePerson.class;//获取到person的成员变量数组(只能获取到public修饰的)Field[] fields = rePersonClass.getFields();/*** public java.lang.Integer com.example.demo.JDK8.Reflection.RePerson.id* public java.lang.String com.example.demo.JDK8.Reflection.RePerson.name* public java.lang.Boolean com.example.demo.JDK8.Reflection.RePerson.flag*/for (Field field : fields) {System.out.println(field);}

根据指定字段名来获取相应的字段

Class<RePerson> rePersonClass = RePerson.class;//根据指定字段名来获取相应的字段Field id = rePersonClass.getField("id");Field name = rePersonClass.getField("name");Field flag = rePersonClass.getField("flag");RePerson rePerson = new RePerson();/*** 传入一个对象来获取值*/Object idvalue = id.get(rePerson);Object namevalue = name.get(rePerson);Object flagvalue = flag.get(rePerson);/*** 这些默认值都是null*/System.out.println(idvalue);//nullSystem.out.println(namevalue);//nullSystem.out.println(flagvalue);//null/*** 设置值* public void set(Object obj, Object value)*/id.set(rePerson, 1);name.set(rePerson, "dzl");flag.set(rePerson, true);System.out.println(rePerson);//RePerson(id=1, name=dzl, flag=true)

去除访问权限

Class<RePerson> rePersonClass = RePerson.class;//获取所有成员变量(不考虑修饰符)Field[] declaredFields = rePersonClass.getDeclaredFields();for (Field declaredField : declaredFields) {/*** public java.lang.Integer com.example.demo.JDK8.Reflection.RePerson.id* public java.lang.String com.example.demo.JDK8.Reflection.RePerson.name* public java.lang.Boolean com.example.demo.JDK8.Reflection.RePerson.flag* private java.lang.Double com.example.demo.JDK8.Reflection.RePerson.price*/System.out.println(declaredField);}RePerson rePerson = new RePerson();Field price = rePersonClass.getDeclaredField("price");//忽略权限修饰符的安全检查(暴力反射)price.setAccessible(true);Object pricevalue = price.get(rePerson);System.out.println(pricevalue);

3.获取成员方法

无参方法

Class<RePerson> rePersonClass = RePerson.class;RePerson rePerson = new RePerson();//获取方法对象Method eat = rePersonClass.getMethod("eat");//方法对象执行方法Object invoke = eat.invoke(rePerson);//eat方法执行了

有参方法

Class<RePerson> rePersonClass = RePerson.class;RePerson rePerson = new RePerson();//获取方法对象Method eat = rePersonClass.getMethod("eat", String.class);//方法对象执行方法Object invoke = eat.invoke(rePerson, "饭饭");//eat了饭饭

获取所有的方法

Class<RePerson> rePersonClass = RePerson.class;RePerson rePerson = new RePerson();Method[] methods = rePersonClass.getMethods();for (Method method : methods) {//暴力破解method.setAccessible(true);System.out.println(method);//获取方法名System.out.println(method.getName());}

4.获取构造器

Class<RePerson> rePersonClass = RePerson.class;//根据构造方法的参数获取相应的构造器Constructor<RePerson> constructor = rePersonClass.getConstructor(Integer.class, String.class, Boolean.class, Double.class);//public com.example.demo.JDK8.Reflection.RePerson(java.lang.Integer,java.lang.String,java.lang.Boolean,java.lang.Double)System.out.println(constructor);RePerson dzl = constructor.newInstance(1, "dzl", true, 12.1);System.out.println(dzl);//RePerson(id=1, name=dzl, flag=true, price=12.1)//直接使用class对象的空参构造创建对象RePerson rePerson = rePersonClass.newInstance();System.out.println(rePerson);//RePerson(id=null, name=null, flag=null, price=null)

5.哪些类型有 Class 对象

//外部类Class<String> cls1 = String.class;//接口(序列化接口)Class<Serializable> cls2 = Serializable.class;//数组Class<Integer[]> cls3 = Integer[].class;Class<int[]> aClass = int[].class;//二维数组Class<float[][]> cls4 = float[][].class;//注解Class<Deprecated> cls5 = Deprecated.class;//枚举Class<Thread.State> cls6 = Thread.State.class;//基本数据类型Class<Long> cls7 = long.class;//void 数据类型Class<Void> cls8 = void.class;//类类型Class<Class> cls9 = Class.class;System.out.println(cls1);System.out.println(cls2);System.out.println(cls3);System.out.println(cls4);System.out.println(cls5);System.out.println(cls6);System.out.println(cls7);System.out.println(cls8);System.out.println(cls9);

如果觉得《反射反射反射》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。