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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yinbolove576 中级黑马   /  2013-8-2 22:02  /  1204 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杨兴庭 于 2013-8-5 18:51 编辑

ReflectPoint代码如下:
  1. package com.itheima.day1;

  2. import java.util.Date;

  3. public class ReflectPoint {
  4.         private Date birthday  = new Date();
  5.         public Date getBrithday() {
  6.                 return birthday;
  7.         }

  8.         public void setBrithday(Date brithday) {
  9.                 this.birthday = brithday;
  10.         }

  11.         private int x;
  12.         public int y;
  13.         
  14.         public String str1 = "ball";
  15.         public String str2 = "basketball";
  16.         public String str3 = "itcast";
  17.         
  18.         public int getX() {
  19.                 return x;
  20.         }

  21.         public void setX(int x) {
  22.                 this.x = x;
  23.         }

  24.         public int getY() {
  25.                 return y;
  26.         }

  27.         public void setY(int y) {
  28.                 this.y = y;
  29.         }

  30.         public ReflectPoint(int x, int y) {
  31.                 super();
  32.                 this.x = x;
  33.                 this.y = y;
  34.         }

  35.         @Override
  36.         public int hashCode() {
  37.                 final int prime = 31;
  38.                 int result = 1;
  39.                 result = prime * result + x;
  40.                 result = prime * result + y;
  41.                 return result;
  42.         }

  43.         @Override
  44.         public boolean equals(Object obj) {
  45.                 if (this == obj)
  46.                         return true;
  47.                 if (obj == null)
  48.                         return false;
  49.                 if (getClass() != obj.getClass())
  50.                         return false;
  51.                 ReflectPoint other = (ReflectPoint) obj;
  52.                 if (x != other.x)
  53.                         return false;
  54.                 if (y != other.y)
  55.                         return false;
  56.                 return true;
  57.         }

  58.         @Override
  59.         public String toString() {
  60.                 return "ReflectPoint [str1=" + str1 + ", str2=" + str2 + ", str3="
  61.                                 + str3 + "]";
  62.         }
  63.         
  64. }
复制代码
IntroSpectorTest1代码如下:
  1. package com.itheima.day1;

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

  8. import org.apache.commons.beanutils.BeanUtils;

  9. public class IntroSpectorTest1 {
  10.         public static void main(String[] args)throws Exception {
  11.                 // TODO Auto-generated method stub
  12.                 ReflectPoint pt1 = new ReflectPoint(3,5);
  13.                
  14.                 String propertyName = "x";
  15.                
  16.                 Object retVal = getProperty(pt1, propertyName);
  17.                 System.out.println(retVal);
  18.                
  19.                 Object value = 7;
  20.                
  21.                 //通过BeanUtils工具包可以直接对相应对象进行getProperty()和setProperty()操作
  22.                 setProperty(pt1, propertyName, value);
  23.                 System.out.println(BeanUtils.getArrayProperty(pt1, "x").getClass().getName());
  24.                 BeanUtils.setProperty(pt1,"x","9");
  25.                 System.out.println(pt1.getX());//setProperty设置某个对象的哪个属性,并将其设置成某个值
  26.                
  27.                 BeanUtils.setProperty(pt1, "birthday.time", "111");
  28.                 System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));
  29.         }

  30.         //设置value的值
  31.         private static void setProperty(Object pt1, String propertyName,
  32.                         Object value) throws IntrospectionException,
  33.                         IllegalAccessException, InvocationTargetException {
  34.                 PropertyDescriptor pd2 = new PropertyDescriptor(propertyName,pt1.getClass());
  35.                 Method methodSetX = pd2.getWriteMethod();
  36.                 methodSetX.invoke(pt1, value);
  37.         }

  38.         //获取methodGetX的值
  39.         private static Object getProperty(Object pt1, String propertyName)
  40.                         throws IntrospectionException, IllegalAccessException,
  41.                         InvocationTargetException {
  42.                 PropertyDescriptor pd = new PropertyDescriptor(propertyName,pt1.getClass());
  43.                 Method methodGetX = pd.getReadMethod();
  44.                 Object retVal = methodGetX.invoke(pt1);
  45.                 return retVal;
  46.         }

  47. }
复制代码
然而就是在运行的时候,总是提示报错,报错详情总是提示System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));这行有问题,
想让大牛帮忙看看到底是问题错在哪儿了?谢谢!

360截图20130802215857638.jpg (33.56 KB, 下载次数: 8)

360截图20130802215857638.jpg

评分

参与人数 1技术分 +1 收起 理由
杜光 + 1

查看全部评分

3 个回复

倒序浏览
这么多代码看着眼晕,建议下次再发代码的时候先说说你的代码是想要实现什么功能,并多加注解,谢谢!
回复 使用道具 举报
杨玲 发表于 2013-8-2 22:05
这么多代码看着眼晕,建议下次再发代码的时候先说说你的代码是想要实现什么功能,并多加注解,谢谢! ...

主要功能就是通过BeanUtils工具包去setProperty()和getProperty(),而出错点就在IntroSpectorTest1中的30、31行,按理应该能直接获取其设置的值"111",但结果却是java.lang.NoSuchMethodException: Unknown property 'birthday'。。。出错点就在31行,貌似我前面的birthday没有设置错的...
回复 使用道具 举报
你这行代码中:
System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));
getProperty方法接收的第二个参数你的传入不对,因为在你的类中根本就没有birthday.time这个属性,你看看你的代码有get或者setbirthday.time这个方法吗?记住BeanUtils是操作Bean对象的属性的,你没有这个属性,自然的调用会出错了。

评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马