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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© murder_fol 中级黑马   /  2013-10-18 22:02  /  1157 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 murder_fol 于 2013-10-19 15:53 编辑
  1. import java.beans.*;
  2. class Test
  3. {
  4.         private int age;
  5.         private String name;
  6.         public  int getAge()
  7.         {
  8.                 return age;
  9.         }
  10.         public  void setAge(int age)
  11.         {
  12.                 this.age = age;
  13.         }
  14.         public String getName()
  15.         {
  16.                 return name;
  17.         }
  18.         public void setName(String name)
  19.         {
  20.                 this.name=name;
  21.         }
  22.         
  23. }



  24. class  IntrospectorDemo
  25. {
  26.         public static void main(String[] args)  throws Exception
  27.         {
  28.                 Test t = new Test();
  29.                 BeanInfo bi = Introspector.getBeanInfo(t.getClass());
  30.                 PropertyDescriptor [] pds = bi.getPropertyDescriptors();
  31.                 System.out.println(pds.length);
  32.                 for(PropertyDescriptor pd :pds)
  33.                 {
  34.                         String name = pd.getName();
  35.                         String type =pd.getPropertyType().getName();
  36.                         System.out.println(type);
  37.                         System.out.println(name);
  38.                 }
  39.         }
  40. }
复制代码
今天写这个代码,无意中发现,只有两个bean属性,结果得到的pds中却有三个,另外一个是java.lang.Class  名字是class。不知道大家发现没有,
getPropertyDescriptor为什么要返回java.lang.Class呢?有什么用途吗?

评分

参与人数 1技术分 +1 收起 理由
李江 + 1 勤学爱问,好榜样

查看全部评分

2 个回复

倒序浏览
getPropertyDescriptor方法获取属性是根据该类的get方法 而不是看你定义了哪些属性,IntrospectorDemo类从Object中继承了getClass方法,所以会获得class属性
如下代码也能得到相同的结果
  1. import java.beans.*;
  2.     class Test
  3.     {
  4.          
  5.             public  int getAge()
  6.             {
  7.                     return 12;
  8.             }
  9.          
  10.             public String getName()
  11.             {
  12.                     return "张三";
  13.             }
  14.          
  15.            
  16.     }



  17. public    class  IntrospectorDemo
  18.     {
  19.             public static void main(String[] args)  throws Exception
  20.             {
  21.                     Test t = new Test();
  22.                     BeanInfo bi = Introspector.getBeanInfo(t.getClass());
  23.                     PropertyDescriptor [] pds = bi.getPropertyDescriptors();
  24.                     System.out.println(pds.length);
  25.                     for(PropertyDescriptor pd :pds)
  26.                     {
  27.                             String name = pd.getName();
  28.                             String type =pd.getPropertyType().getName();
  29.                             System.out.println(type);
  30.                             System.out.println(name);
  31.                     }
  32.             }
  33.     }
复制代码

评分

参与人数 1技术分 +1 收起 理由
周志龙 + 1 赞一个!

查看全部评分

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