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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 北冥有鱼 中级黑马   /  2014-3-20 20:55  /  1603 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class IntrospectorDemo {

  2.         public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  3.                 // TODO Auto-generated method stub
  4.                 IntrospectorDemo demo=new IntrospectorDemo();
  5.                 demo.setName("pengfeilu");
  6.                 //内省
  7.                 BeanInfo bi=Introspector.getBeanInfo(demo.getClass());
  8.                 PropertyDescriptor[] pd=bi.getPropertyDescriptors();
  9.                
  10.                 Object retval=null;
  11.                 for (PropertyDescriptor propertyDescriptor : pd) {
  12.                         //String name=propertyDescriptor.getName();
  13.                         Method method=propertyDescriptor.getReadMethod();
  14.                         retval= method.invoke(demo);
  15.                         System.out.println((String)retval);
  16.                 }
  17.         }
  18.         String name=null;
  19.         String getName() {
  20.                 return name;
  21.         }
  22.         void setName(String name) {
  23.                 this.name = name;
  24.         }

  25. }
复制代码
只是简单的调用内省,获取变量的get方法。调用方法获取name……//结果:Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.String        at com.itheima.IntrospectorDemo.main(IntrospectorDemo.java:25)


评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

6 个回复

倒序浏览
本帖最后由 乔钰博 于 2014-3-20 21:21 编辑

我给你整理了下
  1. class Test{
  2.          public static void main(String[] args) throws Exception{
  3.          // TODO Auto-generated method stub
  4.          IntrospectorDemo demo=new IntrospectorDemo();
  5.          demo.setName("pengfeilu");
  6.          //内省
  7.          BeanInfo bi=Introspector.getBeanInfo(demo.getClass());
  8.          PropertyDescriptor[] pd=bi.getPropertyDescriptors();
  9.          
  10.      
  11.          for (PropertyDescriptor propertyDescriptor : pd) {
  12.                  Method method=propertyDescriptor.getReadMethod();
  13.                  if(propertyDescriptor.getName().equals("name")){
  14.                          System.out.println(method.invoke(demo));
  15.                  }
  16.                  
  17.          }
  18. }
  19. }

  20. public class IntrospectorDemo {

  21.         private String name;
  22.         
  23.         public String getName() {
  24.                 return name;
  25.         }
  26.         public void setName(String name) {
  27.                 this.name = name;
  28.         }

  29. }
复制代码


set和get方法前面要上public就好了

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 syw02014 于 2014-3-20 21:41 编辑

不知道是不是你想要的结果:
  1. import java.beans.BeanInfo;
  2. import java.beans.IntrospectionException;
  3. import java.beans.Introspector;
  4. import java.beans.PropertyDescriptor;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;

  7. public class IntrospectorDemo {

  8.         public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  9.                 // TODO Auto-generated method stub
  10.                 IntrospectorDemo demo=new IntrospectorDemo();
  11.                 demo.setName("pengfeilu");
  12.                 //内省
  13.                 BeanInfo bi=Introspector.getBeanInfo(demo.getClass());
  14.                 PropertyDescriptor[] pd=bi.getPropertyDescriptors();
  15.                
  16.                 Object retval=null;
  17.                 for (PropertyDescriptor propertyDescriptor : pd) {
  18.                         //String name=propertyDescriptor.getName();
  19.                         Method method=propertyDescriptor.getReadMethod();
  20.                          retval= method.invoke(demo);
  21.                          System.out.println(retval);
  22.                 }
  23.         }
  24.         private String name=null;
  25.         public String getName() {
  26.                 return name;
  27.         }
  28.         public void setName(String name) {
  29.                 this.name = name;
  30.         }

  31. }
复制代码

运行结果:
class IntrospectorDemo
pengfeilu

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报

还是,不知道问题出哪儿了

  1. class Test {

  2.         public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  3.                 // TODO Auto-generated method stub
  4.                 IntrospectorDemo demo=new IntrospectorDemo();
  5.                 demo.setName("pengfeilu");
  6.                 //内省
  7.                 BeanInfo bi=Introspector.getBeanInfo(demo.getClass());
  8.                 PropertyDescriptor[] pd=bi.getPropertyDescriptors();
  9.                
  10.                 Object retval=null;
  11.                 for (PropertyDescriptor propertyDescriptor : pd) {
  12.                         //String name=propertyDescriptor.getName();
  13.                         Method method=propertyDescriptor.getReadMethod();
  14.                         retval=method.invoke(demo);
  15.                         System.out.println((String)retval);
  16.                 }
  17.         }

  18. }
  19. public class IntrospectorDemo {
  20.        
  21.         private String name=null;
  22.        
  23.         public String getName() {
  24.                 return name;
  25.         }
  26.        
  27.         public void setName(String name) {
  28.                 this.name = name;
  29.         }
  30. }
复制代码
回复 使用道具 举报
北冥有鱼 发表于 2014-3-20 22:09
还是,不知道问题出哪儿了

System.out.println((String)retval); 类型转换错误啊,那个异常都说了ClassCastException
你直接打印retval,或者用retval.toString()
回复 使用道具 举报
乔钰博 发表于 2014-3-20 22:13
System.out.println((String)retval); 类型转换错误啊,那个异常都说了ClassCastException
你直接打印ret ...

看————藤椅那一楼,,我打印出来也是那样,,,多了一个
回复 使用道具 举报
syw02014 发表于 2014-3-20 21:26
不知道是不是你想要的结果:
运行结果:
class IntrospectorDemo

知不知道为什么会出现 第一行的结果
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马