黑马程序员技术交流社区

标题: 内省,重构出来的方法却不是想要的方法? [打印本页]

作者: 陈虹旭    时间: 2013-3-1 12:43
标题: 内省,重构出来的方法却不是想要的方法?
  1. public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
  2.         {
  3.                 Person p1 = new Person();
  4.                
  5.                 String property1 = "name";
  6.                
  7.                 PropertyDescriptor pd = getProperty(p1, property1);
  8.                
  9.                 Object value = "zhangsan";
  10.                 setProperty(p1, property1, value);
  11.         }

  12.         private static void setProperty(Person p1, String property1, Object value)
  13.                         throws IntrospectionException, IllegalAccessException,
  14.                         InvocationTargetException
  15.         {
  16.                 PropertyDescriptor pd2 = new PropertyDescriptor(property1,p1.getClass());
  17.                 Method setN = pd2.getWriteMethod();
  18.                 setN.invoke(p1, value);
  19.         }

  20. <font color="red">//这里,我想知道为什么按照张老师的说法去做的,而我重构出来的方法返回值是PropertyDescriptor呢?</font>
  21. private static PropertyDescriptor getProperty(Object p1, String property1)
  22.           throws IntrospectionException, IllegalAccessException,
  23.                         InvocationTargetException
  24.         {
  25.                 PropertyDescriptor pd = new PropertyDescriptor(property1,p1.getClass());
  26.                 Method getN = pd.getReadMethod();
  27.                 Object retVal = getN.invoke(p1);
  28.                 return pd;
  29.         }
复制代码
谁知道这个是怎么回事 ,麻烦帮忙解决下啊?

作者: 谢洋    时间: 2013-3-1 12:52
26.        {

27.                PropertyDescriptor pd = new PropertyDescriptor(property1,p1.getClass());

28.                Method getN = pd.getReadMethod();

29.                Object retVal = getN.invoke(p1);//你是不是想返回这个?

30.                return pd;//这些返回的是PropertyDescriptor

31.        }
作者: 陈虹旭    时间: 2013-3-1 13:13
谢洋 发表于 2013-3-1 12:52
26.        {

27.                PropertyDescriptor pd = new PropertyDescriptor(property ...

对 你说的没错  我也明白应该是返回retVal  我是说我重构过程中  显示的就直接是返回属性描述器  你说是不是eclipse的问题?
作者: 谢洋    时间: 2013-3-1 13:42
陈虹旭 发表于 2013-3-1 13:13
对 你说的没错  我也明白应该是返回retVal  我是说我重构过程中  显示的就直接是返回属性描述器  你说是 ...

哦,我刚才没有看明白你的意思,应该是
你是把代码写成这样,才重构的?
PropertyDescriptor pd = new PropertyDescriptor(property1,p1.getClass());

28.                Method getN = pd.getReadMethod();

29.                Object retVal = getN.invoke(p1);
还是写成这样,就重构了?
PropertyDescriptor pd = new PropertyDescriptor(property1,p1.getClass());





作者: 陈虹旭    时间: 2013-3-1 13:53
谢洋 发表于 2013-3-1 13:42
哦,我刚才没有看明白你的意思,应该是
你是把代码写成这样,才重构的?
PropertyDescriptor pd = new Pr ...

当然是把这三行都选中之后重构的啊
作者: 谢洋    时间: 2013-3-1 14:08
陈虹旭 发表于 2013-3-1 13:53
当然是把这三行都选中之后重构的啊

我猜eclipse是根据第一行来,确定返回结果的吧,可以写代码测下,但我觉得也没这个必要了在这纠着的

作者: 陈虹旭    时间: 2013-3-1 14:50
谢洋 发表于 2013-3-1 14:08
我猜eclipse是根据第一行来,确定返回结果的吧,可以写代码测下,但我觉得也没这个必要了在这纠着的
...

嗯 我觉得也是  我自己给改下好了  呵呵  谢谢你啊! 费了你半天功夫  :lol




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