黑马程序员技术交流社区

标题: 关于内省 [打印本页]

作者: 北冥有鱼    时间: 2014-3-20 20:55
标题: 关于内省
  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)



作者: 乔钰博    时间: 2014-3-20 21:20
本帖最后由 乔钰博 于 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就好了
作者: syw02014    时间: 2014-3-20 21:26
本帖最后由 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


作者: 北冥有鱼    时间: 2014-3-20 22:09
乔钰博 发表于 2014-3-20 21:20
我给你整理了下

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

  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:13
北冥有鱼 发表于 2014-3-20 22:09
还是,不知道问题出哪儿了

System.out.println((String)retval); 类型转换错误啊,那个异常都说了ClassCastException
你直接打印retval,或者用retval.toString()
作者: 北冥有鱼    时间: 2014-3-20 22:43
乔钰博 发表于 2014-3-20 22:13
System.out.println((String)retval); 类型转换错误啊,那个异常都说了ClassCastException
你直接打印ret ...

看————藤椅那一楼,,我打印出来也是那样,,,多了一个
作者: 北冥有鱼    时间: 2014-3-20 22:44
syw02014 发表于 2014-3-20 21:26
不知道是不是你想要的结果:
运行结果:
class IntrospectorDemo

知不知道为什么会出现 第一行的结果




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2