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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© m529031854 中级黑马   /  2014-7-12 12:05  /  832 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.zhujl.fanshe;

  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;

  4. public class ReflectTester
  5. {
  6.         // 该方法实现对Customer对象的拷贝操作
  7.         public Object copy(Object object) throws Exception
  8.         {
  9.                 Class<?> classType = object.getClass();
  10.                 //这句话 是获取构造方法的实例吗?
  11.                 Object objectCopy = classType.getConstructor(new Class[] {})
  12.                                 .newInstance(new Object[] {});

  13.                 // 获得对象的所有成员变量

  14.                 Field[] fields = classType.getDeclaredFields();

  15.                 for (Field field : fields)
  16.                 {
  17.                         String name = field.getName();

  18.                         String firstLetter = name.substring(0, 1).toUpperCase();// 将属性的首字母转换为大写

  19.                         String getMethodName = "get" + firstLetter + name.substring(1);
  20.                         String setMethodName = "set" + firstLetter + name.substring(1);

  21.                         Method getMethod = classType.getMethod(getMethodName,
  22.                                         new Class[] {});
  23.                         Method setMethod = classType.getMethod(setMethodName,
  24.                                         new Class[] { field.getType() });
  25.                         //这个不理解,通过反射调用get方法,为什么传的是Oject,而不是classType.newInstance
  26.                         Object value = getMethod.invoke(object, new Object[] {});
  27.                         System.out.println(value);
  28.                         //这个也不理解,为什么第一个参数这个传入的是构造方法对象实例?第二个参数为什么事value
  29.                         setMethod.invoke(objectCopy, new Object[] { value });
  30.                 }

  31.                 // 以上两行代码等价于下面一行
  32.                 // Object obj2 = classType.newInstance();

  33.                 // System.out.println(obj);

  34.                 return objectCopy;
  35.         }

  36.         public static void main(String[] args) throws Exception
  37.         {
  38.                 Customer customer = new Customer("Tom", 20);
  39.                 customer.setId(1L);

  40.                 ReflectTester test = new ReflectTester();

  41.                 Customer customer2 = (Customer) test.copy(customer);

  42.                 System.out.println(customer2.getId() + "," + customer2.getName() + ","
  43.                                 + customer2.getAge());
  44.         }
  45. }

  46. class Customer
  47. {
  48.         private Long id;

  49.         private String name;

  50.         private int age;

  51.         public Customer()
  52.         {

  53.         }

  54.         public Customer(String name, int age)
  55.         {
  56.                 this.name = name;
  57.                 this.age = age;
  58.         }

  59.         public Long getId()
  60.         {
  61.                 return id;
  62.         }

  63.         public void setId(Long id)
  64.         {
  65.                 this.id = id;
  66.         }

  67.         public String getName()
  68.         {
  69.                 return name;
  70.         }

  71.         public void setName(String name)
  72.         {
  73.                 this.name = name;
  74.         }

  75.         public int getAge()
  76.         {
  77.                 return age;
  78.         }

  79.         public void setAge(int age)
  80.         {
  81.                 this.age = age;
  82.         }
  83. }
复制代码
问题在代码中间,谢谢大家,菜鸟新手

1 个回复

倒序浏览
额,没人回答
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马