- package com.zhujl.fanshe;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- public class ReflectTester
- {
- // 该方法实现对Customer对象的拷贝操作
- public Object copy(Object object) throws Exception
- {
- Class<?> classType = object.getClass();
- //这句话 是获取构造方法的实例吗?
- Object objectCopy = classType.getConstructor(new Class[] {})
- .newInstance(new Object[] {});
- // 获得对象的所有成员变量
- Field[] fields = classType.getDeclaredFields();
- for (Field field : fields)
- {
- String name = field.getName();
- String firstLetter = name.substring(0, 1).toUpperCase();// 将属性的首字母转换为大写
- String getMethodName = "get" + firstLetter + name.substring(1);
- String setMethodName = "set" + firstLetter + name.substring(1);
- Method getMethod = classType.getMethod(getMethodName,
- new Class[] {});
- Method setMethod = classType.getMethod(setMethodName,
- new Class[] { field.getType() });
- //这个不理解,通过反射调用get方法,为什么传的是Oject,而不是classType.newInstance
- Object value = getMethod.invoke(object, new Object[] {});
- System.out.println(value);
- //这个也不理解,为什么第一个参数这个传入的是构造方法对象实例?第二个参数为什么事value
- setMethod.invoke(objectCopy, new Object[] { value });
- }
- // 以上两行代码等价于下面一行
- // Object obj2 = classType.newInstance();
- // System.out.println(obj);
- return objectCopy;
- }
- public static void main(String[] args) throws Exception
- {
- Customer customer = new Customer("Tom", 20);
- customer.setId(1L);
- ReflectTester test = new ReflectTester();
- Customer customer2 = (Customer) test.copy(customer);
- System.out.println(customer2.getId() + "," + customer2.getName() + ","
- + customer2.getAge());
- }
- }
- class Customer
- {
- private Long id;
- private String name;
- private int age;
- public Customer()
- {
- }
- public Customer(String name, int age)
- {
- this.name = name;
- this.age = age;
- }
- public Long getId()
- {
- return id;
- }
- public void setId(Long id)
- {
- this.id = id;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public int getAge()
- {
- return age;
- }
- public void setAge(int age)
- {
- this.age = age;
- }
- }
复制代码 问题在代码中间,谢谢大家,菜鸟新手
|
|