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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 党传才 中级黑马   /  2012-4-21 21:07  /  1398 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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[] {});
                System.out.println(objectCopy  );

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

                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() });

                        Object value = getMethod.invoke(object, new Object[] {});

                        setMethod.invoke(objectCopy, new Object[] { value });
                }
                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;
        }
}

其中:        Object value = getMethod.invoke(object, new Object[] {});

                        setMethod.invoke(objectCopy, new Object[] { value });
这两行语句是什么意思??脑袋都大了

2 个回复

倒序浏览
Object value = getMethod.invoke(object, new Object[] {});
调用原对象的getMethod()方法
setMethod.invoke(objectCopy, new Object[] { value });
调用拷贝对象的setMethod()方法
回复 使用道具 举报
Method getMethod = classType.getMethod(getMethodName,new Class[] {});
Method setMethod = classType.getMethod(setMethodName,new Class[] { field.getType() });
以上获取类中属性的get和set方法的名称getMethod、setMethod
Object value = getMethod.invoke(object, new Object[] {});  //对象object 调用getMethod方法时并向其传入new Object[] {}而得到的返回值是value
                      setMethod.invoke(objectCopy, new Object[] { value });

执行该Method.invoke方法的参数是执行这个方法的对象object,和参数数组 new Object[] {}
可以这么理解:object对象中带有参数new Object[] {}的method方法。返回值是Object,也既是该方法的返回值
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马