• 使用反射复制一个JavaBean的对象


    先看代码吧:

    1. package com.java.study;  
    2.   
    3. import java.io.Serializable;  
    4. import java.lang.reflect.Field;  
    5. import java.lang.reflect.InvocationTargetException;  
    6. import java.lang.reflect.Method;  
    7.   
    8. public class ReflectTester {  
    9.     public Object copy(Object object) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {  
    10.         Class classType = object.getClass();//获得对象的类型  
    11.         System.out.println("Class:" + classType.getName());  
    12.           
    13.         //通过默认的构造函数创建一个新的对象  
    14.         Object objectCopy = classType.getConstructor(new Class[] {}).  
    15.         newInstance(new Object[] {});  
    16.           
    17.         //获得对象的所有属性  
    18.         Field fields[] = classType.getDeclaredFields();  
    19.           
    20.         for(int i = 0; i < fields.length; i++) {  
    21.             Field field = fields[i];  
    22.               
    23.             String fieldName = field.getName();  
    24.             String firstLetter = fieldName.substring(0,1).toUpperCase();  
    25.             //获得和属性对应的getXXX()方法的名字  
    26.             String getMethodName = "get" + firstLetter + fieldName.substring(1);  
    27.             //获得和属性对应的setXXX()方法的名字  
    28.             String setMethodName = "set" + firstLetter + fieldName.substring(1);  
    29.               
    30.             //获得和属性对应的getXXX()方法  
    31.             Method getMethod = classType.getMethod(getMethodName, new Class[]{});  
    32.             //获得和属性对应的setXXX()方法  
    33.             Method setMethod = classType.getMethod(setMethodName, new Class[]{field.getType()});  
    34.               
    35.             //调用原对象的getXXX()方法  
    36.             Object value = getMethod.invoke(object, new Object[]{});  
    37.             System.out.println(fieldName + ":" + value);  
    38.             //调用复制对象的setXXX()方法  
    39.             setMethod.invoke(objectCopy, new Object[]{value});  
    40.         }  
    41.         return objectCopy;  
    42.           
    43.     }  
    44.       
    45.     public static void main(String[] args) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {  
    46.         Customer1 customer = new Customer1("tom",21);  
    47.         customer.setId(new Long(1));  
    48.           
    49.         Customer1 customerCopy = (Customer1) new ReflectTester().copy(customer);  
    50.         System.out.println("Copy information:" + customerCopy.getName()+ ""  
    51.                 +customerCopy.getAge());  
    52.     }  
    53.   
    54. }  
    55.   
    56. class Customer1 implements Serializable {  
    57.       
    58.     private Long id;  
    59.     private String name;  
    60.     private int age;  
    61.       
    62.     public Customer1() {}  
    63.     public Customer1(String name, int age){  
    64.         this.name = name;  
    65.         this.age = age;  
    66.     }  
    67.       
    68.     public Long getId() {  
    69.         return id;  
    70.     }  
    71.     public void setId(Long id) {  
    72.         this.id = id;  
    73.     }  
    74.     public String getName() {  
    75.         return name;  
    76.     }  
    77.   
    78.     public void setName(String name) {  
    79.         this.name = name;  
    80.     }  
    81.   
    82.     public int getAge() {  
    83.         return age;  
    84.     }  
    85.   
    86.     public void setAge(int age) {  
    87.         this.age = age;  
    88.     }  
    89. }  


    代码中有一个Customer1的JavaBean,通过反射复制了一个Customer1类的对象,为customerCopy,程序中的new Class[] { }是一种快捷方式,获得方法的引用。[]中用来放参数,{}里面可以放置方法的返回类型。new Class[] { }相当于new class("构造函数参数").newinstance.

    //调用原对象的getXXX()方法
          Object value = getMethod.invoke(object, new Object[]{});
          System.out.println(fieldName + ":" + value);


    用来获得特定对象的特定方法。

  • 相关阅读:
    【译】NodeJS and Good Practices
    【译】单一责任原则
    CSS 属性 z-index
    Node 连接 MySql
    CentOS 7 后台克隆远程库
    H5log打印
    利用Promise实现Promise.all
    Set、Map、WeakSet、WeakMap 理解与使用
    vue如何禁止弹窗后面的滚动条滚动?
    vue面试题总结
  • 原文地址:https://www.cnblogs.com/dongxiaoguang/p/2979607.html
Copyright © 2020-2023  润新知