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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  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.         }
复制代码
谁知道这个是怎么回事 ,麻烦帮忙解决下啊?

6 个回复

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

嗯 我觉得也是  我自己给改下好了  呵呵  谢谢你啊! 费了你半天功夫  :lol
回复 使用道具 举报
陈虹旭 发表于 2013-3-1 13:53
当然是把这三行都选中之后重构的啊

我猜eclipse是根据第一行来,确定返回结果的吧,可以写代码测下,但我觉得也没这个必要了在这纠着的
回复 使用道具 举报
谢洋 发表于 2013-3-1 13:42
哦,我刚才没有看明白你的意思,应该是
你是把代码写成这样,才重构的?
PropertyDescriptor pd = new Pr ...

当然是把这三行都选中之后重构的啊
回复 使用道具 举报
陈虹旭 发表于 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 12:52
26.        {

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

对 你说的没错  我也明白应该是返回retVal  我是说我重构过程中  显示的就直接是返回属性描述器  你说是不是eclipse的问题?
回复 使用道具 举报
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.        }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马