Java反射机制总结之二

来源:互联网 发布:类似美工的职业 编辑:程序博客网 时间:2024/05/17 09:18

Java反射机制总结之一

1.要想使用反射,首先需要获得待处理类或对象所对应的Class对象。

2.获取某个类或对象所对应的Class地下的常用三中方法

3.若通过类的不带参数的构造方法来生成对象,我们有两种方式:

 3.1

 3.2 

4.若想通过带参数的构造方法生成实例,必须采用3.2的方法才可以。

5.ReflectTest类进一步演示了Reflection APIs的基本使用方法。ReflectTest类有一个copy(Object object)方法,这个方法能够创建一个和参数object同样类型的对象,然后把object对象中的所有属性拷贝到新建的对象中,并将它返回。

这个代码示例只能复制简单的JavaBean,假定JavaBean的每隔属性都有public类型的getXXX()和setXXX()方法。

代码示例:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. import java.lang.reflect.Constructor;  
  2. import java.lang.reflect.Field;  
  3. import java.lang.reflect.Method;  
  4. public class ReflectTest {  
  5.    //该方法实现对Customer对象的拷贝操作    
  6.     public Object copy(Object object)throws Exception{  
  7.         Class<?>  classtype = object.getClass();  
  8.         //System.out.println(classtype.getName());  
  9.         // 使用空构造方法生成示例对象  
  10.         Constructor cons = classtype.getConstructor(new Class[]{});  
  11.         Object objectCopy = cons.newInstance(new Object[]{});  
  12.         //以上两行等价于这一行:Object objectCopy = classtype.newInstance();  
  13.           
  14.         //使用带参数的构造方法生成示例对象  
  15.         //Constructor cons = classtype.getConstructor(new Class[]{String.class,int.class});  
  16.         //Object obj = cons.newInstance(new Object[]{"hello",3});  
  17.         Field[] fields = classtype.getDeclaredFields();  
  18.         for(Field field:fields){  
  19.             String name = field.getName();  
  20.             //将属性的首字母转化为大写  
  21.             String firstLetter = name.substring(01).toUpperCase();  
  22.             String getMethodName ="get"+firstLetter+name.substring(1);  
  23.             String setMethodName = "set"+firstLetter+name.substring(1);  
  24.             Method getMethod = classtype.getMethod(getMethodName, new Class[]{});  
  25.             Method setMethod = classtype.getMethod(setMethodName,new Class[]{field.getType()});  
  26.             Object value = getMethod.invoke(object, new Object[]{});  
  27.             setMethod.invoke(objectCopy, new Object[]{value});            
  28.         }  
  29.         return objectCopy;  
  30.     }  
  31.     public  static void main(String[] args)throws Exception{  
  32.         Customer customer = new Customer("Tom",20);  
  33.         customer.setId(1L);  
  34.         ReflectTest test= new ReflectTest();  
  35.         Customer customer2 = (Customer)test.copy(customer);  
  36.         System.out.println(customer2.getId()+","+customer2.getName()+","+customer2.getAge());  
  37.           
  38.         }  
  39. }  
  40.   
  41. class Customer{  
  42.     private Long id;  
  43.     private String name;  
  44.     private int age;  
  45.     public Customer(){  
  46.           
  47.     }  
  48.     public Customer(String name, int age){  
  49.         this.name = name;  
  50.         this.age = age;  
  51.     }  
  52.     public Long getId() {  
  53.         return id;  
  54.     }  
  55.     public void setId(Long id) {  
  56.         this.id = id;  
  57.     }  
  58.     public String getName() {  
  59.         return name;  
  60.     }  
  61.     public void setName(String name) {  
  62.         this.name = name;  
  63.     }  
  64.     public int getAge() {  
  65.         return age;  
  66.     }  
  67.     public void setAge(int age) {  
  68.         this.age = age;  
  69.     }  
  70. }  
运行结果:

1,Tom,20

本文参考:

第六十二讲 Class类、Method类及Field类的使用:http://www.iqiyi.com/w_19rr26kpe1.html

第六十三讲 反射机制大总结:http://www.iqiyi.com/w_19rr26m6gp.html
0 0