黑马程序员技术交流社区

标题: java 反射 + 类型转换??? [打印本页]

作者: 刘林虎    时间: 2013-4-9 22:29
标题: java 反射 + 类型转换???
本帖最后由 刘林虎 于 2013-4-9 22:31 编辑
  1. public void handleMessage(Message msg) {
  2. TransactionBundle args = (TransactionBundle) msg.boj;
  3. int transactionType = args.getTransactionType();
  4. ...
  5. }
  6. 现在我要实现上面这个方法里面的代码,但是温室是TransactionBundle这个类只能通过反射的方式加载,要怎么实现上面的效果呢?
  7. 求大神指教!!!
复制代码

作者: 牛海亮    时间: 2013-4-10 00:09
  1. package cn.itcast.reflect;
  2. import java.lang.reflect.*;
  3. import java.util.Arrays;


  4. public class ReflectTest
  5. {

  6.         public static void main(String[] args)  throws Exception
  7.         {
  8.                 Person p = new Person("张三",12);       
  9.                 getObject(p);
  10.         }
  11.         //通过反射获得属性对应的get方法,传入对象,即可打印出所有属性对应的值。
  12.         public static void getObject(Object object)
  13.         {
  14.                 try
  15.                 {
  16.                         //获取传进来的映射对象
  17.                         Class c = object.getClass();
  18.                         //取得对象的所有属性,放到一个数组中
  19.                         Field[]  f = c.getDeclaredFields();
  20.                         for(int i = 0; i<f.length; i++)
  21.                         {
  22.                                 //获取对象的属性
  23.                                 String fieldName = f[i].getName();
  24.                                 //拼接出来访问器名字
  25.                                 String name = "get"+firUpCase(fieldName);
  26.                                 //System.out.println(name);
  27.                                 //获取名字是name的方法。
  28.                                 Method m = c.getMethod(name);
  29.                                 //映射后的对象的方法  对象调用invoke()方法,实现它在原对象中对应的方法,这里实现的是get方法。
  30.                                 System.out.println(m.invoke(object));                               
  31.                         }               
  32.                        
  33.                 } catch (Exception e) {
  34.                         e.printStackTrace();
  35.                 }
  36.                
  37.         }
  38.         public static String firUpCase(String fieldName)
  39.         {
  40.                 String changed = null;
  41.                 int temp = 0;
  42.                 char [] fn = fieldName.toCharArray();
  43.                 temp = (int)fn[0]-32;
  44.                 if(temp>64&&temp<91)
  45.                 {
  46.                                
  47.                 fn[0] = (char)temp;
  48.                 }
  49.                 changed = new String(fn);
  50.                 return changed;
  51.         }

  52. }
复制代码

作者: 牛海亮    时间: 2013-4-10 00:09
  1. package cn.itcast.reflect;

  2. public class Person {

  3.         private String name;
  4.         private int age;
  5.         public Person(String name, int age) {
  6.                 super();
  7.                 this.name = name;
  8.                 this.age = age;
  9.         }
  10.         public Person() {
  11.                 super();
  12.                 // TODO Auto-generated constructor stub
  13.         }
  14.         public String getName() {
  15.                 return name;
  16.         }
  17.         public void setName(String name) {
  18.                 this.name = name;
  19.         }
  20.         public int getAge() {
  21.                 return age;
  22.         }
  23.         public void setAge(int age) {
  24.                 this.age = age;
  25.         }
  26.        
  27. }
复制代码

作者: 牛海亮    时间: 2013-4-10 00:15
这是一个通过反射获取Person类属性值的例子,希望能对你有帮助。
firUpCase()这个方法是为了把字符串首字母转换成大写的。
作者: 牛海亮    时间: 2013-4-10 00:22
你把args传进getObject(args),它对应的所有属性的值就都出来了.要想获得某个属性的值,可在该函数第一行后写if(fieldName.equals(属性名称))将其筛选出来。
作者: 牛海亮    时间: 2013-4-10 00:26
  1. //具体写法为
  2.                 if(fieldName.equals(属性名称)))
  3.                 {

  4.                         //拼接出来访问器名字
  5.                         String name = "get"+firUpCase(fieldName);
  6.                         //System.out.println(name);
  7.                         //获取名字是name的方法。
  8.                         Method m = c.getMethod(name);
  9.                         //映射后的对象的方法  对象调用invoke()方法,实现它在原对象中对应的方法,这里实现的是get方法。
  10.                         System.out.println(m.invoke(object));
  11.                 }
复制代码

作者: 黄玉昆    时间: 2013-4-11 19:50
如果问题未解决,请继续追问,如果没有问题了,请将帖子分类 改为“已解决”,谢谢




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