zoukankan      html  css  js  c++  java
  • 使用反射复制对象

    package reflect;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class InvokeTest {
        public static Object copy(Object object) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
            Class cls = object.getClass();
            System.out.println(cls.getName());
            
            Object objectCopy = cls.getConstructor(new Class[]{}).newInstance(new Object[]{});
            
            //Field[] fields = cls.getFields();
            Field[] fields = cls.getDeclaredFields();
            for(Field field : fields) {
                System.out.println(field.getName());
                String fieldName = field.getName();
                String firstChar = fieldName.substring(0, 1).toUpperCase();
                //获得get set方法名
                String getMethodName = "get" + firstChar + fieldName.substring(1);
                String setMethodName = "set" + firstChar + fieldName.substring(1);
                //获得对应方法
                Method getMethod = cls.getMethod(getMethodName, new Class[]{});
                Method setMethod = cls.getMethod(setMethodName, new Class[]{field.getType()});
                System.out.println(getMethod.getName() + "," + setMethod.getName());
                
                //调用get方法
                Object value = getMethod.invoke(object, new Object[]{});
                //调用set方法
                setMethod.invoke(objectCopy, value);
            }
            return objectCopy;
        }
        
        public static void main(String[] args) throws SecurityException, IllegalArgumentException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
            Customer customer = new Customer(23, 1, "zhangsan");
            Customer clone = (Customer)copy(customer);
            System.out.println(clone.getId() + "," + clone.getAge() + "," + clone.getName());
        }
    }
    
    
    class Customer {
        private int id;
        private String name;
        private int age;
        
        public Customer(){}
        
        public Customer(int age, int id, String name) {
            super();
            this.age = age;
            this.id = id;
            this.name = name;
        }
        public int getId() {
            return id;
        }
        public void setId(int 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;
        }
        
        
    }
  • 相关阅读:
    在sql server中怎样获得正在执行的Sql查询
    在windows中使用VMWare安装Mac OS 10.7
    Scrspy 命令
    Windows Service 小品
    线程同步(一)
    线程基础必知必会(二)
    线程基础必知必会(一)
    准备工作与简介
    Python 正则表达式急速入门
    SQL Server 每日一题--每月销售额
  • 原文地址:https://www.cnblogs.com/jerry19890622/p/3287148.html
Copyright © 2011-2022 走看看