测试 java反射Reflection中 获取 类属性、方法、构造方法及 调用类方法改变 对象属性

 1 package com.li.test;
 2 /**
 3  * 实体类User
 4  * @author Administrator
 5  */
 6 public class User {
 7     
 8     /**
 9      * name
10      */
11     private String name;//name
12     private int age;//age
13 
14     public User() {
15     }
16     public User(String name,int age) {
17         this.name = name;
18         this.age = age;
19     }
20 
21     public String getName() {
22         return name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27     }
28 
29     public int getAge() {
30         return age;
31     }
32 
33     public void setAge(int age) {
34         this.age = age;
35     }
36 }
 1 package com.li.test;
 2 
 3 import java.lang.reflect.Constructor;
 4 import java.lang.reflect.Field;
 5 import java.lang.reflect.InvocationTargetException;
 6 import java.lang.reflect.Method;
 7 
 8 /**
 9  * 测试 java反射Reflection中 获取 类属性、方法、构造方法及 调用类方法改变 对象属性
10  * 
11  * @author Administrator
12  * 
13  */
14 public class TestReflect {
15 
16     /**
17      * @param args
18      */
19     public static void main(String[] args) {
20         // 获取 类属性
21         Field[] field = User.class.getDeclaredFields();
22         System.out.println("User类属性----begin-----");
23         for (int i = 0; i < field.length; i++) {
24             System.out.println(field[i].getName());
25         }
26         System.out.println("User类属性----end-----");
27         // 获取 类方法
28         Method method[] = User.class.getDeclaredMethods();
29         System.out.println("User类方法----begin-----");
30         for (int i = 0; i < method.length; i++) {
31             System.out.println(method[i].getName());
32         }
33         System.out.println("User类方法----end-----");
34         // 获取 构造方法
35         Constructor constructor[] = User.class.getDeclaredConstructors();
36         System.out.println("User类构造方法----begin-----");
37         for (int i = 0; i < constructor.length; i++) {
38             System.out.println(constructor[i]);
39         }
40         System.out.println("User类构造方法----end-----");
41         // 改变属性值
42         User user1 = new User("A", 1);
43         System.out.println("begin " + user1.getName() + user1.getAge());
44         Field fieldName = null;
45         try {
46             fieldName = User.class.getDeclaredField("name");
47         } catch (SecurityException e1) {
48             // TODO Auto-generated catch block
49             e1.printStackTrace();
50         } catch (NoSuchFieldException e1) {
51             // TODO Auto-generated catch block
52             e1.printStackTrace();
53         }
54         Method setNameMethod = null;
55         Method getNameMethod = null;
56         Object name = null;
57         try {
58             setNameMethod = user1.getClass().getDeclaredMethod("setName",new Class[]{fieldName.getType()});
59             getNameMethod = user1.getClass().getDeclaredMethod("getName",new Class[]{});
60         } catch (SecurityException e) {
61             e.printStackTrace();
62         } catch (NoSuchMethodException e) {
63             e.printStackTrace();
64         }
65         try {
66             setNameMethod.invoke(user1, new Object[]{"B"});
67             name = getNameMethod.invoke(user1, new Class[]{});
68             
69         } catch (IllegalArgumentException e) {
70             // TODO Auto-generated catch block
71             e.printStackTrace();
72         } catch (IllegalAccessException e) {
73             // TODO Auto-generated catch block
74             e.printStackTrace();
75         } catch (InvocationTargetException e) {
76             // TODO Auto-generated catch block
77             e.printStackTrace();
78         }
79 
80         System.out.println("end " + user1.getName() + user1.getAge());
81         System.out.println("end " + (String)name);
82     }
83 
84 }

 

以下是控制台输出:

User类属性----begin-----
name
age
User类属性----end-----
User类方法----begin-----
getAge
setAge
getName
setName
User类方法----end-----
User类构造方法----begin-----
public com.li.test.User()
public com.li.test.User(java.lang.String,int)
User类构造方法----end-----
begin A1
end B1
end B

posted @ 2012-11-09 16:30  Java碎碎念  阅读(409)  评论(0编辑  收藏  举报