内省对象 用的少,被BeanUtils代替

描述

BeanInfo

对JavaBean进行描述的接口

Introspector

描述所有的JavaBean的成员类

PropertyDescriptor

描述的是JavaBean的属性类

shape.java

 1 package reflect;
 2 
 3 public class Shape {
 4     private int x=4;
 5     int y;
 6     protected int z;
 7     public int h;
 8     public Shape() {
 9     }
10     public Shape(int x, Integer y) {
11         super();
12         this.x = x;
13         this.y = y;
14 
15     }
16     public Shape(int x, int y, int z, int h) {
17         super();
18         this.x = x;
19         this.y = y;
20         this.z = z;
21         this.h = h;
22     }
23     public int getX() {
24         return x;
25     }
26     public void setX(int x) {
27         this.x = x;
28     }
29     public int getY() {
30         return y;
31     }
32     public void setY(int y) {
33         this.y = y;
34     }
35     public int getZ() {
36         return z;
37     }
38     public void setZ(int z) {
39         this.z = z;
40     }
41     public int getH() {
42         return h;
43     }
44     public void setH(int h) {
45         this.h = h;
46     }
47     @Override
48     public String toString() {
49         return "Shape [x=" + x + ", y=" + y + ", z=" + z + ", h=" + h
50                 + ", getX()=" + getX() + ", getY()=" + getY() + ", getZ()="
51                 + getZ() + ", getH()=" + getH() + ", getClass()=" + getClass()
52                 + ", hashCode()=" + hashCode() + ", toString()="
53                 + super.toString() + "]";
54     }
55     
56 }
 1         Shape shape=new Shape();
 2         BeanInfo beaninfo=Introspector.getBeanInfo(Shape.class);
 3         PropertyDescriptor []pds=beaninfo.getPropertyDescriptors();
 4         //返回5个,其中还有一个是Object中的 getClass()和setClass(), 它是根据 getXxx和setXxx找的
 5         System.out.println(pds.length);
 6         PropertyDescriptor pd = pds[1];            //数组下标 按照字典顺序  第一个是 class、h、x、y、z
 7         System.out.println(pd.getName());
 8         Method setMethod=pd.getWriteMethod();
 9         System.out.println(setMethod.getName());
10         setMethod.invoke(shape, 33);
11         System.out.println(pds[1].getReadMethod().invoke(shape,null));

结果:

5
h
setH
33

你可能感兴趣的:(BeanUtils)