内省(个人笔记)

什么内省?

    个人认为内省和反射有密不可分的关系,当你拿到某个类的时候,却不知道该类具体有什么的时候,可以通过内省来获取该属性。

     Student类:

     

 1 package javaBean;
 2 
 3 public class Student {
 4     //创建字段
 5     private String name ="张子健" ;
 6     private int age;
 7     
 8     
 9     public String getName() {
10         return name;
11     }
12     public void setName(String name) {
13         this.name = name;
14     }
15     public int getAge() {
16         return age;
17     }
18     public void setAge(int age) {
19         this.age = age;
20     }
21     public String Aderss() {
22         return "address";
23     }
24     @Override
25     public String toString() {
26         return "Student [name=" + name + ", age=" + age + "]";
27     }
28     
29     
30 }
Student

       测试类:

 

 1 package javaBean;
 2 
 3 import java.beans.BeanInfo;
 4 import java.beans.Introspector;
 5 import java.beans.PropertyDescriptor;
 6 import java.lang.reflect.Method;
 7 import java.util.HashMap;
 8 import java.util.Map;
 9 
10 import org.apache.commons.beanutils.BeanUtils;
11 import org.junit.Test;
12 
13 public class TestDemo {
14     
15     @Test
16     public void test() throws Exception{
17         //获取Student的所有属性描述器,含父类继承下的
18         BeanInfo info = Introspector.getBeanInfo(Student.class);
19         //获取Student的所有属性描述器
20         PropertyDescriptor[] pdi = info.getPropertyDescriptors();
21         //for
22         System.out.println(pdi.length);
23         for(PropertyDescriptor pd :pdi){
24             System.out.println(pd.getName());
25         }
26         
27     }
28 
29     @Test
30     public void test2() throws Exception{
31         //获取Student的所有属性描述器,不含父类继承下的
32         BeanInfo info = Introspector.getBeanInfo(Student.class,Object.class);
33         //获取Student的所有属性描述器
34         PropertyDescriptor[] pdi = info.getPropertyDescriptors();
35         //for
36         System.out.println(pdi.length);
37         for(PropertyDescriptor pd :pdi){
38             System.out.println(pd.getName());
39         }
40         
41     }
42     @Test
43     public void test3() throws Exception{
44         //获取属性并进行修改
45         PropertyDescriptor descriptor = new PropertyDescriptor("name", Student.class);
46         Method method = descriptor.getReadMethod();//获得了Student的getName()
47         Student s =new Student();
48         
49         String value = (String)method.invoke(s, null);
50         System.out.println(value);
51         //设置属性
52         Method method2 = descriptor.getWriteMethod();
53 
54         method2.invoke(s, "陈帅帅");
55         System.out.println(s.getName());
56         
57         
58     }
59     //内省的方式,前提是需要导包
60     @Test
61     public void test4() throws Exception{
62         //首先创建对象
63         Student s =new Student();
64         //内省的方式
65         String bean = BeanUtils.getProperty(s, "name");
66         System.out.println(bean);
67         //修改属性
68         BeanUtils.setProperty(s, "name", "李超");
69         System.out.println(s.getName());
70     }
71     //反射和内省的实际效果,假设Student 什么也不知道
72     @Test
73     public void test5() throws Exception{
74         //我们要了解其中有什么,不包含其父亲属性?获取其所有的属性
75          BeanInfo beanInfo = Introspector.getBeanInfo(Student.class,Object.class);
76          //获取其所有的属性数组
77          PropertyDescriptor[] des = beanInfo.getPropertyDescriptors();
78          //查看其个数
79          System.out.println(des.length);
80          for(PropertyDescriptor p :des){
81              System.out.println(p.getName());
82          }
83          //好,现在加入给你一个Map集合,要你吧集合中的值添加到Student中去,该怎么做?
84          Map<String ,Object>  map =new HashMap<String ,Object>();
85          map.put("key1", "张三");
86          map.put("key1", 13);
87          Student s = new Student();
88          //利用内省 炒鸡简单
89          BeanUtils.populate(s, map);
90          System.out.println(s);
91         
92     }
93 }
TestDemo

 

    当使用JavaBean时候需要导包:

    

 

posted @ 2017-09-26 21:36  独自在  阅读(129)  评论(0编辑  收藏  举报