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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 坚持525 中级黑马   /  2014-11-5 20:33  /  742 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 坚持525 于 2014-11-5 20:50 编辑

程序编译时可以的,运行出现了这个,怎么回事?
  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. import org.apache.commons.beanutils.BeanUtils;

  8. class Person {
  9.         private String name;
  10.         private int age;
  11.         public String getName() {
  12.                 return name;
  13.         }

  14.         public void setName(String name) {
  15.                 this.name = name;
  16.         }

  17.         public int getAge() {
  18.                 return age;
  19.         }

  20.         public void setAge(int age) {
  21.                 this.age = age;
  22.         }
  23. }

  24. public class BeansTest {

  25.         public static void main(String[] args) throws Exception {
  26.                 Person p = new Person();
  27.                 p.setName("王赫");
  28.                 String propertiesName = "name";
  29.                 String name = extracted(p, propertiesName);// 演示了用eclipse抽取方法
  30.                 System.out.println(name);
  31.                 String propertiesAge = "age";
  32.                 int age = 23;
  33.                 SetAge(p, propertiesAge, age);
  34.                 String name1 = BeanUtils.getProperty(p, "name");
  35.                 // 使用beanUtils工具包进行获取和设置属性(尽管这些属性是私有的,可是有方法啊,是不是很方便)
  36.                 System.out.println(BeanUtils.getProperty(p, "name").getClass()
  37.                                 .getName());
  38.                 System.out.println(name1);

  39.                 BeanUtils.setProperty(p, "age", 19);
  40.                 System.out.println(p.getAge());
  41.         }

  42.         private static void SetAge(Person p, String propertiesAge, int age)
  43.                         throws IntrospectionException, IllegalAccessException,
  44.                         InvocationTargetException {
  45.                 PropertyDescriptor bp1 = new PropertyDescriptor(propertiesAge,
  46.                                 p.getClass());
  47.                 Method methodSetAge = bp1.getWriteMethod();
  48.                 methodSetAge.invoke(p, age);
  49.                 System.out.println(p.getAge());

  50.         }

  51.         private static String extracted(Object p, String propertiesName)
  52.                         throws IntrospectionException, IllegalAccessException,
  53.                         InvocationTargetException {
  54.                 /*
  55.                  * PropertyDescriptor bp = new PropertyDescriptor(propertiesName,
  56.                  * p.getClass()); Method methodGetName = bp.getReadMethod(); Object
  57.                  * readVal = methodGetName.invoke(p); System.out.println(readVal);
  58.                  */

  59.                 BeanInfo beanInfo = Introspector.getBeanInfo(p.getClass());
  60.                 PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
  61.                 Object retVal = null;
  62.                 for (PropertyDescriptor pd : pds) {
  63.                         if (pd.getName().equals(propertiesName)) {
  64.                                 Method methodGetX = pd.getReadMethod();
  65.                                 retVal = (String) methodGetX.invoke(p);
  66.                                 break;

  67.                         }
  68.                 }
  69.                 return (String) retVal;
  70.         }
  71. }
复制代码



问题2.jpg (113.46 KB, 下载次数: 1)

问题2.jpg

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马