反射

来源:互联网 发布:加工中心手动编程铣圆 编辑:程序博客网 时间:2024/05/23 10:21
反射:当一个字节码文件加载到内存的时候,JVM会对该字节码进行解剖,然后创建一个对象的Class对象,把字节码文件的信息全部都存储到该Class对象中,我们只要获取到Class对象,我们就可以使用字节码对象创建对象、设置对象的属性或者调用对象的方法等操作。
Java提供了三种方式获取类的字节码:
    forName("包名.类名")   forName方法用于加载某个类的字节码到内存中,并使用class对象进行封装。
    类名.class
    对象.getClass()
注意:在反射技术中,一个类的任何成员都有对应的类进行描述。例如:成员变量(Field类) 方法(Method类) 
  1. package com.cn.reflect;
  2. public class Person {
  3. private int id;
  4. String name;
  5. public Person(int id, String name) {
  6. this.id = id;
  7. this.name = name;
  8. }
  9. private Person() {
  10. }
  11. public void eat(){
  12. System.out.println(name + "高高兴兴吃饭。。。");
  13. }
  14. public void eat(int num){
  15. System.out.println(name + "高高兴兴吃了" + num + "大碗饭。。。");
  16. }
  17. private void sleep(){
  18. System.out.println(name + "偷偷睡觉。。。");
  19. }
  20. private static void work(){
  21. System.out.println("努力工作。。。");
  22. }
  23. public void sum(int[] arr){
  24. System.out.println("长度是" + arr.length);
  25. }
  26. @Override
  27. public String toString() {
  28. return "编号" + this.id + "\t姓名:" + this.name;
  29. }
  30. }
3种方式获取Class对象:
  1. package com.cn.reflect;
  2. /**
  3. * Author:Liu Zhiyong(QQ:1012421396)
  4. * Version:Version_1
  5. * Date:2016年8月26日21:59:48
  6. * Desc:
  7. 反射:当一个字节码文件加载到内存的时候,JVM会对该字节码进行解剖,然后创建一个对象的Class对象,把字节码文件的信息全部都存储到该Class对象中,我们只要获取到Class对象,我们就可以使用字节码对象设置对象的属性或者调用对象的方法等操作。
  8. Java提供了三种方式获取类的字节码:
  9. forName("包名.类名") forName方法用于加载某个类的字节码到内存中,并使用class对象进行封装。
  10. 类名.class
  11. 对象.getClass()
  12. 注意:在反射技术中,一个类的任何成员都有对应的类进行描述。例如:成员变量(Field类) 方法(Method类)
  13. */
  14. public class Demo1 {
  15. Person p;
  16. public static void main(String[] args) throws ClassNotFoundException {
  17. //Person p = new Person(110, "木丁西");
  18. //推荐使用:获取Class对象的方式1:
  19. Class c1 = Class.forName("com.cn.reflect.Person");
  20. System.out.println(c1);
  21. //获取Class对象的方式2:通过类名获取
  22. Class c2 = Person.class;
  23. System.out.println(c2);
  24. //获取Class对象的方式3:通过对象获取
  25. //Class c3 = new Person().getClass();
  26. //System.out.println("c1 == c2 == c3 吗?" + (c1==c2 && c2==c3));
  27. }
  28. }
通过Class对象获取构造方法:
  1. package com.cn.reflect;
  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.InvocationTargetException;
  4. /**
  5. * Author:Liu Zhiyong(QQ:1012421396)
  6. * Version:Version_1
  7. * Date:2016年8月26日22:27:30
  8. * Desc:
  9. 如何通过Class对象获取构造方法。
  10. */
  11. public class Demo2 {
  12. public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  13. //获取到对应的Class对象
  14. Class c1 = Class.forName("com.cn.reflect.Person");
  15. //通过Class对象获取对应的构造方法
  16. Constructor[] constructors = c1.getConstructors(); //获取此类所有公共的构造方法
  17. for(Constructor c : constructors){
  18. System.out.println(c);
  19. }
  20. Constructor[] constructors2 = c1.getDeclaredConstructors(); //获取此类的所有的构造方法,包括私有的在内
  21. for(Constructor c : constructors2){
  22. System.out.println(c);
  23. }
  24. Constructor<Person> c = c1.getConstructor(int.class, String.class);//获取单个的指定的构造方法
  25. Person p = (Person)c.newInstance(120, "刘先森"); //newInstance() 创建一个对象
  26. Constructor<Person> c2 = c1.getDeclaredConstructor();//获取单个的指定的构造方法
  27. System.out.println(c2);
  28. c2.setAccessible(true);//设置
  29. Person p2 = c2.newInstance();
  30. System.out.println(p2);
  31. }
  32. }
在反射技术中使用了Method类描述方法:
  1. package com.cn.reflect;
  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.lang.reflect.Method;
  5. /**
  6. * Author:Liu Zhiyong(QQ:1012421396)
  7. * Version:Version_1
  8. * Date:2016年8月27日10:38:40
  9. * Desc:
  10. 通过Class对象获取到对应的方法。
  11. 在反射技术中使用了Method类描述方法。
  12. */
  13. public class Demo3 {
  14. public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
  15. //获取对应 的Class对象
  16. Class c = Class.forName("com.cn.reflect.Person");
  17. //获取所有公共的方法
  18. Method[] methods = c.getMethods();//getMethods()获取所有公共的方法,包括父类的
  19. for(Method m : methods){
  20. System.out.println(m);
  21. }
  22. System.out.println("==================================");
  23. Method[] methods2 = c.getDeclaredMethods();//getDeclaredMethods()获取到所有的方法,但是不包含父类的方法
  24. for(Method m : methods2){
  25. System.out.println(m);
  26. }
  27. System.out.println("==================================");
  28. Method method = c.getMethod("eat", null);
  29. System.out.println(method);
  30. System.out.println("==================================");
  31. Method method2 = c.getMethod("eat", int.class);
  32. System.out.println(method2);
  33. System.out.println("==================================");
  34. Constructor constructor = c.getConstructor(int.class, String.class);
  35. Person p = (Person)constructor.newInstance(120, "小木");
  36. method2.invoke(p, 3);//invoke() 第一个参数:方法的调用对象。 第二个参数:方法所需要的参数。
  37. System.out.println("==================================");
  38. //执行私有的方法
  39. Method method3 = c.getDeclaredMethod("sleep", null);//获取私有方法
  40. method3.setAccessible(true);//设置访问权限允许访问
  41. method3.invoke(p, null);
  42. System.out.println("==================================");
  43. //执行静态的私有的方法
  44. Method method4 = c.getDeclaredMethod("work", null);
  45. method4.setAccessible(true);//设置访问权限允许访问
  46. method4.invoke(null, null);//如果底层方法是静态的,那么可以忽略指定的 obj 参数。该参数可以为 null。
  47. System.out.println("==================================");
  48. //数组类型获取
  49. Method method5 = c.getMethod("sum", int[].class);
  50. method5.invoke(p, new int[]{1, 3, 5, 7, 9});
  51. }
  52. }
在反射技术中使用了Field类描述了成员变量:
package com.cn.reflect;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;/*** Author:Liu Zhiyong(QQ:1012421396)* Version:Version_1* Date:2016年8月27日11:09:50* Desc:通过反射获取对应的成员变量在反射技术中使用了Field类描述了成员变量。*/public class Demo4 {public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException {//获取到对应的Class对象Class c = Class.forName("com.cn.reflect.Person");//获取到所有的成员变量Field[] fields = c.getDeclaredFields();              //fields = c.getFields();//获取所有的公共的成员变量,包括父类的for(Field f : fields){System.out.println(f);}System.out.println("=========================");//获取一个对象Constructor constructor = c.getConstructor(int.class, String.class);Person p = (Person)constructor.newInstance(120, "木丁西");//获取单个的成员变量Field field = c.getDeclaredField("id");//设置访问权限可以访问field.setAccessible(true);field.set(p, 155);//第一个参数:设置该数据的成员变量。第二个参数:属性值。System.out.println(p);}}


0 0
原创粉丝点击