黑马程序员技术交流社区

标题: 张老师讲的JavaBean的一个小问题 [打印本页]

作者: 王杰    时间: 2012-4-26 21:23
标题: 张老师讲的JavaBean的一个小问题
  1. package cn.itcast.day1;

  2. import java.beans.BeanInfo;
  3. import java.beans.IntrospectionException;
  4. import java.beans.Introspector;
  5. import java.beans.MethodDescriptor;
  6. import java.beans.PropertyDescriptor;
  7. import java.lang.reflect.InvocationTargetException;
  8. import java.lang.reflect.Method;

  9. public class IntroSpectorTest {

  10.         /**
  11.          * IntroSpector:内省。
  12.          * @param args
  13.          */
  14.         public static void main(String[] args) throws Exception{
  15.                 // TODO Auto-generated method stub
  16.                 ReflectPoint pt1 = new ReflectPoint(2, 3);
  17. //ReflectPoint类是自己定义的。看过视屏的都知道吧
  18.                
  19.                 String propertyName = "x";
  20.                
  21.                 Object retVal = getProperty(pt1, propertyName);
  22.                 System.out.println(retVal);
  23.                
  24.                 Object val = 76;
  25.                 setProperty(pt1, propertyName, val);
  26.                 System.out.println(pt1.getX());
  27.                
  28.                
  29.         }

  30.         private static void setProperty(Object pt1, String propertyName,
  31.                         Object val) throws IntrospectionException, IllegalAccessException,
  32.                         InvocationTargetException {
  33.                 PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
  34.                 Method methodSet = pd.getWriteMethod();
  35.                 methodSet.invoke(pt1, val);
  36.         }

  37.         private static Object getProperty(Object pt1, String propertyName)
  38.                         throws IntrospectionException, IllegalAccessException,
  39.                         InvocationTargetException {
  40.                 PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
  41.                 Object retVal = pd.getReadMethod().invoke(pt1);
  42.                
  43.                 /*[b]以下这个方法是张老师演示的,可是我运行的结果为null,并不是2!哪里错了?????[/b]
  44.                 Object retVal = null;
  45.                 BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
  46.                 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  47.                
  48.                 for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
  49.                          if(propertyDescriptor.getName().equals(propertyName))
  50.                                 retVal = propertyDescriptor.getReadMethod().invoke(pt1);
  51.                          break;
  52.                 }*/
  53.                 return retVal;
  54.         }

  55. }
复制代码

作者: 张小庆    时间: 2012-4-26 21:32
                Object val = 76;

                setProperty(pt1, propertyName, val);

                System.out.println(pt1.getX());//应该是你这段代码的问题吧,你给x赋了个Object啊,我感觉是这的原因


作者: 王杰    时间: 2012-4-26 22:27
张小庆 发表于 2012-4-26 21:32
Object val = 76;

                setProperty(pt1, propertyName, val);

不是这个原因,我就是把实现方式用遍历的方式,我改变了内部实现,外部没有影响。
作者: 王杰    时间: 2012-4-27 14:53
顶一下

作者: 丁佼    时间: 2012-4-27 15:35
本帖最后由 丁佼 于 2012-4-27 15:36 编辑

if(propertyDescriptor.getName().equals(propertyName))
     retVal = propertyDescriptor.getReadMethod().invoke(pt1);
break;

你第一次盘算之后,不管属性是否已经找到,都把循环break了,retval还没获取到值,当然就是null。
把if下的语句{}括起来就可以了。。。

老师,这么长的代码我一句一句看、分析。。您看着办。。。
作者: 邵中国    时间: 2012-4-27 17:35
我运行了一下你的代码,结果 是2   76

作者: 王杰    时间: 2012-4-29 00:02
邵中国 发表于 2012-4-27 17:35
我运行了一下你的代码,结果 是2   76

注意!问题代码在注释里
作者: 王杰    时间: 2012-4-29 00:39
丁佼 发表于 2012-4-27 15:35
if(propertyDescriptor.getName().equals(propertyName))
     retVal = propertyDescriptor.getReadMethod ...

啊 大意 了  谢谢了




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