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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张亚青 中级黑马   /  2013-3-27 12:39  /  2814 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  1.                 BeanInfo beanInfo=Introspector.getBeanInfo(classPerson);
  2.                 PropertyDescriptor [] pds=beanInfo.getPropertyDescriptors();
  3.                 Object result=new Object();
  4.                 for(PropertyDescriptor pd:pds)
  5.                         if(pd.getName().equals("setAge"))
  6.                         {
  7.                                 Method methodSetAge=pd.getWriteMethod();
  8.                                 result=methodSetAge.invoke(p,25);
  9.                         }
复制代码
以上代码中methodSetAge.invoke(p,25)返回的值为一个Object对象,如果我想要Person类型的对象,为什么
p=(Person)result;
这样转化的时候会报错????
我要怎么做?

2 个回复

倒序浏览
以上代码中methodSetAge.invoke(p,25)返回的值为一个Object对象,如果我想要Person类型的对象,为什么
p=(Person)result;
这样转化的时候会报错????
我要怎么做?

回答:methodSetAge.invoke(p,25)是写方法,是没有返回值的,这里也只会返回null, 所以不能进行p=(Person)result;
         你想要有返回值,你只能用 读方法。Method methodGet=pd.getReadMethod();
                                result= methodGet.invoke(p);//这才是读方法。但要注意,读方法是没有参数的。
  1. package test;

  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. public class Test6 {

  9.         /**
  10.          * @param args
  11.          * @throws IntrospectionException
  12.          * @throws InvocationTargetException
  13.          * @throws IllegalAccessException
  14.          * @throws IllegalArgumentException
  15.          */
  16.         public static void main(String[] args) throws IntrospectionException,
  17.                         IllegalArgumentException, IllegalAccessException,
  18.                         InvocationTargetException {
  19.                 Person p = new Person(20, "zhang");
  20.                 System.out.println("改变前:" + p);
  21.                 BeanInfo beanInfo = Introspector.getBeanInfo(p.getClass());
  22.                 PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
  23.                 Object result = new Object();
  24.                 for (PropertyDescriptor pd : pds) {
  25.                         //System.out.println("pd.getName()="+pd.getName());//这里的pd.getName返回的是属性名字。age,name
  26.                         if (pd.getName().equals("age")) {
  27.                                 //1
  28.                                 Method methodSetAge = pd.getWriteMethod();//这里你是获得的写方法setAge。
  29.                                 result = methodSetAge.invoke(p, 25);//还要注意你的setAge方法是没有返回值的。所以result=null
  30.                                 //2
  31.                                 Method methodGet=pd.getReadMethod();
  32.                                 result= methodGet.invoke(p);//这才是读方法。但要注意,读方法是没有参数的。
  33.                                
  34.                                 //提示,你的result的值是你的Person的属性值。而不是Person对象。你怎么把它转成Person
  35.                                 System.out.println("result="+result);
  36.                                 System.out.println("改变后:" + p);
  37.                         }

  38.                 }
  39.         }

  40. }

  41. class Person {
  42.         int age = 3;
  43.         String name = "zzz";

  44.         public int getAge() {
  45.                 return age;
  46.         }

  47.         public void setAge(int age) {
  48.                 this.age = age;
  49.         }

  50.         public String getName() {
  51.                 return name;
  52.         }

  53.         public void setName(String name) {
  54.                 this.name = name;
  55.         }

  56.         public Person(int age, String name) {
  57.                 this.age = age;
  58.                 this.name = name;

  59.         }

  60.         @Override
  61.         public String toString() {
  62.                 return "Person [age=" + age + ", name=" + name + "]";
  63.         }
  64. }
复制代码
回复 使用道具 举报
本帖最后由 VOIDMAIN 于 2013-3-29 22:53 编辑

methodSetAge.invoke(p,25);只是通过Method的实例对象,也就是methodSetAge去调用方法,被调用的方法是在Method实例化的时候传进去的,也就是pd.getWriteMethod();              

invoke,可以理解为方法的方法;

你要想通过反射获得实例对象,给你个参考写法(如果直接用Person类型的话,需要做类型的强转):
Object  obj=Person.class.getConstructor().newInstance();

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

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