Chinaunix首页 | 论坛 | 博客
  • 博客访问: 951721
  • 博文数量: 168
  • 博客积分: 3853
  • 博客等级: 中校
  • 技术积分: 1854
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-15 23:50
文章分类

全部博文(168)

文章存档

2014年(12)

2013年(46)

2012年(60)

2011年(11)

2010年(1)

2009年(17)

2008年(21)

我的朋友

分类: Java

2011-09-22 13:01:24

1.JAVA中无论生成一个类的多少个对象,这些对象都会对应同一个Class对象.
2.获取某个类或对象所对应的Class对象的常用的3种方式:
   a.使用Class类的静态方法forName,Class.forName("java.lang.String");
   b.使用.class语法:String.class;
   c.使用对象的getClass()方法: String s = "aa"; Class clazz = s.getClass();

3.若想通过类的不带参数的构造方法来生成对象,我们有两种方式:
   a.先将获得Class对象,然后通过该Class对象的newInstance()方法直接生成即可:Class classType = String.class;Object obj = classType.newInstance();
   b.先获得Class对象,然后通过该对象获得对应的Constructor对象,
在通过该Constructor对象的newInstance()方法生成
Java代码 复制代码 收藏代码
  1. package com.jelly.reflect;   
  2.   
  3. import java.lang.reflect.Field;   
  4. import java.lang.reflect.InvocationTargetException;   
  5. import java.lang.reflect.Method;   
  6.   
  7. /**  
  8.  * JAVA反射技术  
  9.  *   
  10.  * @author Jelly QQ:136179492  
  11.  *   
  12.  */  
  13. public class ReflectTester {   
  14.     /**  
  15.      * 通过一个对象利用反射技术将其复制  
  16.      *   
  17.      * @param 参数object  
  18.      * @return 复制的Object对象  
  19.      * @throws Exception  
  20.      */  
  21.     public Object copy(Object object) throws Exception {   
  22.         Class classType = object.getClass();   
  23.         Object obj = classType.getConstructor(new Class[] {}).newInstance(   
  24.                 new Object[] {});   
  25.         Field[] fields = classType.getDeclaredFields();   
  26.         for (Field field : fields) {   
  27.             String name = field.getName();   
  28.             String firstLetter = name.substring(01).toUpperCase();   
  29.             String getMethodName = "get" + firstLetter + name.substring(1);   
  30.             String setMethodName = "set" + firstLetter + name.substring(1);   
  31.             Method getMethod = classType.getMethod(getMethodName,   
  32.                     new Class[] {});   
  33.             Method setMethod = classType.getMethod(setMethodName,   
  34.                     new Class[] { field.getType() });   
  35.             Object value = getMethod.invoke(object, new Object[] {});   
  36.             System.out.println(value);   
  37.             setMethod.invoke(obj, new Object[] { value });   
  38.         }   
  39.         return obj;   
  40.     }   
  41.   
  42.     public static void main(String[] args) throws Exception,   
  43.             InvocationTargetException {   
  44.         ReflectTester test = new ReflectTester();   
  45.         Customer customer = new Customer("jelly"23);   
  46.         Customer obj = (Customer) test.copy(customer);   
  47.         System.out.println(obj.getName() + obj.getAge());   
  48.     }   
  49. }   
  50.   
  51. class Customer {   
  52.     public Customer() {   
  53.   
  54.     }   
  55.   
  56.     public Customer(String name, int age) {   
  57.         this.name = name;   
  58.         this.age = age;   
  59.     }   
  60.   
  61.     private long id;   
  62.     private String name;   
  63.     private int age;   
  64.   
  65.     public long getId() {   
  66.         return id;   
  67.     }   
  68.   
  69.     public void setId(long id) {   
  70.         this.id = id;   
  71.     }   
  72.   
  73.     public String getName() {   
  74.         return name;   
  75.     }   
  76.   
  77.     public void setName(String name) {   
  78.         this.name = name;   
  79.     }   
  80.   
  81.     public int getAge() {   
  82.         return age;   
  83.     }   
  84.   
  85.     public void setAge(int age) {   
  86.         this.age = age;   
  87.     }   
  88. }  
package com.jelly.reflect; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * JAVA反射技术 * * @author Jelly QQ:136179492 * */ public class ReflectTester { /** * 通过一个对象利用反射技术将其复制 * * @param 参数object * @return 复制的Object对象 * @throws Exception */ public Object copy(Object object) throws Exception { Class classType = object.getClass(); Object obj = 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() }); Object value = getMethod.invoke(object, new Object[] {}); System.out.println(value); setMethod.invoke(obj, new Object[] { value }); } return obj; } public static void main(String[] args) throws Exception, InvocationTargetException { ReflectTester test = new ReflectTester(); Customer customer = new Customer("jelly", 23); Customer obj = (Customer) test.copy(customer); System.out.println(obj.getName() + obj.getAge()); } } class Customer { public Customer() { } public Customer(String name, int age) { this.name = name; this.age = age; } private long id; private String name; private int 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; } }
阅读(1185) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~