本帖最后由 张向辉 于 2013-2-5 12:26 编辑
- package cn.itcast.day1;
- import java.beans.BeanInfo;
- import java.beans.Introspector;
- import java.beans.PropertyDescriptor;
- import java.lang.reflect.Method;
- public class IntroSpectorDemo {
- public static void main(String[] args) throws Exception
- {
- ReflectPoint rp1 = new ReflectPoint();
-
-
- BeanInfo info = Introspector.getBeanInfo(rp1.getClass());
- PropertyDescriptor[] pds = info.getPropertyDescriptors();
- for(PropertyDescriptor pd : pds)
- {
- String beanName = pd.getName();
- System.out.println(beanName);
- if(!(beanName.equals("class")))
- getPropertyDescriptor(beanName, rp1);
- }
- }
- private static void getPropertyDescriptor(String x,ReflectPoint rp1) throws Exception
- {
- PropertyDescriptor pd = new PropertyDescriptor(x, rp1.getClass());
- Method mthGet = pd.getReadMethod();
- Method mthSet = pd.getWriteMethod();
- mthSet.invoke(rp1, 222);
- System.out.println(mthGet.invoke(rp1, null));
- }
- }
复制代码- package cn.itcast.day1;
- public class ReflectPoint
- {
- private int x;
- private int XX;
- public int y;
-
- public int getX() {
- return x;
- }
- public int getXX() {
- return XX;
- }
- public void setXX(int xX) {
- XX = xX;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
-
-
- }
复制代码 |
|