A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 张向辉 于 2013-2-5 12:26 编辑
  1. package cn.itcast.day1;

  2. import java.beans.BeanInfo;
  3. import java.beans.Introspector;
  4. import java.beans.PropertyDescriptor;
  5. import java.lang.reflect.Method;

  6. public class IntroSpectorDemo {

  7.         public static void main(String[] args) throws Exception
  8.         {
  9.                 ReflectPoint rp1 = new ReflectPoint();
  10.                
  11.                
  12.                 BeanInfo info = Introspector.getBeanInfo(rp1.getClass());
  13.                 PropertyDescriptor[] pds = info.getPropertyDescriptors();
  14.                 for(PropertyDescriptor pd : pds)
  15.                 {        
  16.                         String beanName = pd.getName();
  17.                         System.out.println(beanName);
  18.                         if(!(beanName.equals("class")))
  19.                                 getPropertyDescriptor(beanName, rp1);
  20.                 }
  21.         }

  22.         private static void getPropertyDescriptor(String x,ReflectPoint rp1) throws Exception
  23.         {
  24.                 PropertyDescriptor pd = new  PropertyDescriptor(x, rp1.getClass());
  25.                 Method mthGet = pd.getReadMethod();
  26.                 Method mthSet = pd.getWriteMethod();
  27.                 mthSet.invoke(rp1, 222);
  28.                 System.out.println(mthGet.invoke(rp1, null));
  29.         }

  30. }
复制代码
  1. package cn.itcast.day1;

  2. public class ReflectPoint
  3. {
  4.         private int x;
  5.         private int XX;
  6.         public int y;
  7.         
  8.         public int getX() {
  9.                 return x;
  10.         }

  11.         public int getXX() {
  12.                 return XX;
  13.         }

  14.         public void setXX(int xX) {
  15.                 XX = xX;
  16.         }

  17.         public void setX(int x) {
  18.                 this.x = x;
  19.         }

  20.         public int getY() {
  21.                 return y;
  22.         }

  23.         public void setY(int y) {
  24.                 this.y = y;
  25.         }


  26.         
  27.         
  28. }
复制代码

未命名.jpg (3.09 KB, 下载次数: 18)

未命名.jpg

评分

参与人数 1黑马币 +9 收起 理由
Rancho_Gump + 9

查看全部评分

1 个回复

倒序浏览
从 Object 继承来了一个 pulic Class<?> getClass(){}方法 所以被看成是属性class 的getter方法 自然也就是属性了

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马