Java关于反射的用法

一. 首先是准备一个需要反射的类

 1 public class Person {
 2     private String name;
 3 
 4     private int age;
 5 
 6     public String sex;
 7 
 8     public int tel;
 9 
10     private Person(String name, int age) {
11         this.name = name;
12         this.age = age;
13     }
14 
15 
16     public Person() {
17 
18     }
19 
20     public Person(String name, int age, String sex, int tel) {
21         this.name = name;
22         this.age = age;
23         this.sex = sex;
24         this.tel = tel;
25     }
26 
27     public String showInfo() {
28         return "name:" + name + "-age:" + age + "-sex:" + sex + "-tel" + tel;
29     }
30 
31     private String getName() {
32 
33         return name;
34     }
35 }

二.现在开始使用反射

    /**
     * 获取一个类的Class对象方法
     *
     * @param view
     * @throws Exception
     */
    public void getClassMethod(View view) throws Exception {
        //获取class对象有三种方法

        Person person = new Person();
        //第一种
        Class class1 = person.getClass();
        //第二种
        Class class2 = Person.class;
        //第三种
        Class class3 = Class.forName("com.adminhj.httpexercise.reflect.Person");

        Log.i(TAG, "class1==class2-->" + (class1 == class2));
        Log.i(TAG, "class2==class3-->" + (class2 == class3));

    }

打印的结果是:

  com.adminhj.httpexercise.reflect.ReflectActivity: class1==class2-->true

com.adminhj.httpexercise.reflect.ReflectActivity: class2==class3-->true 

 1  /**
 2      * 通过反射获取Person类的共有构造方法
 3      *
 4      * @param view
 5      */
 6     public void getConstruct(View view) throws Exception {
 7         //加载person类的class对象
 8         Class class1 = Class.forName("com.adminhj.httpexercise.reflect.Person");
 9         //获取指定的构造方法  public Person(String name, int age, String sex, int tel)
10         // ,如果构造方法中没有参数可不要写如例如 Constructor constructor = class1.getConstructor();
11         Constructor constructor = class1.getConstructor(String.class, int.class, String.class, int.class);
12         //new出指定构造方法的对象
13         Object obj = constructor.newInstance("张三", 22, "", 110);
14         //获取反射类中的公共属性
15         Field sex = class1.getField("sex");
16         Field tel = class1.getField("tel");
17         Log.i(TAG, "sex:" + sex.get(obj) + "-------tel:" + tel.get(obj));
18         //获取反射类中指定的的公共方法
19         Method showInfo = class1.getMethod("showInfo");
20         //执行方法
21         String info = (String) showInfo.invoke(obj);
22         Log.i(TAG, "info" + "-------" + info);
23 
24     }

打印结果:

 1 com.adminhj.httpexercise.reflect.ReflectActivity: sex:男-------tel:110 

 1 com.adminhj.httpexercise.reflect.ReflectActivity: info-------name:张三-age:22-sex:男-tel110 

 1     /**
 2      * 通过反射获取Person类的私有构造方法
 3      *
 4      * @param view
 5      * @throws Exception
 6      */
 7     public void getDeclaredConstruct(View view) throws Exception {
 8         //加载person类的class对象
 9         Class class1 = Class.forName("com.adminhj.httpexercise.reflect.Person");
10         //获取指定的构造方法  private Person(String name, int age)
11         Constructor constructor = class1.getDeclaredConstructor(String.class, int.class);
12         //设置这个私有的构造方法是可进入的,不设置则会报错
13         constructor.setAccessible(true);
14         //创建实例对象
15         Object obj = constructor.newInstance("里斯", 2000);
16 
17 
18         //获取所有属性包括私有,class1.getDeclaredField() 此方法是获取所有属性不包括私有
19         //
20         Field[] declaredFields = class1.getDeclaredFields();
21         for (Field field : declaredFields) {
22             field.setAccessible(true);
23 
24             //注释掉的是在使用私有熟悉的时候需要注意的
25             //field.setAccessible(true); 设置属性可见
26             //field.set(obj, "张飞"); 给属性赋值
27 
28             Log.i(TAG, field.getName() + "-------" + field.get(obj));
29         }
30         //获取反射类中指定的的私有方法
31         Method getNameAndAge = class1.getDeclaredMethod("getNameAndAge");
32         //设置私有方法可执行
33         getNameAndAge.setAccessible(true);
34         //执行方法
35         String result = (String) getNameAndAge.invoke(obj);
36 
37         Log.i(TAG, result);
38     }

打印结果:

com.adminhj.httpexercise.reflect.ReflectActivity: age-------2000
com.adminhj.httpexercise.reflect.ReflectActivity: name-------里斯
com.adminhj.httpexercise.reflect.ReflectActivity: sex-------null
com.adminhj.httpexercise.reflect.ReflectActivity: tel-------0

 1 com.adminhj.httpexercise.reflect.ReflectActivity: name=里斯----------age=2000 

学无止尽 小伙伴们加油哦

原文地址:https://www.cnblogs.com/hejiaoshou/p/7464228.html