Java反射得到属性的值和设置属性的值 (转)

public class UserBean { 
04.    private Integer id; 
05.    private int age; 
06.    private String name; 
07.    private String address; 
08.    
09.    public UserBean(){ 
10.       System.out.println("实例化"); 
11.    } 
12.    
13.    public Integer getId() { 
14.       return id; 
15.    } 
16.    public void setId(Integer id) { 
17.       this.id = id; 
18.    } 
19.    public int getAge() { 
20.       return age; 
21.    } 
22.    public void setAge(int age) { 
23.       this.age = age; 
24.    } 
25.    public String getName() { 
26.       return name; 
27.    } 
28.    public void setName(String name) { 
29.       this.name = name; 
30.    } 
31.    public String getAddress() { 
32.       return address; 
33.    } 
34.    public void setAddress(String address) { 
35.       this.address = address; 
36.    } 
37.    
38.    
39.    
40.} 
41.  
42.2 > 反射测试 
43.  
44.package com.whbs.test; 
45.  
46.import java.lang.reflect.Field; 
47.import java.lang.reflect.Method; 
48.  
49.import com.whbs.bean.UserBean; 
50.  
51.public class Test1 { 
52.  
53.    public static void main(String[] args) throws Exception { 
54.  
55.       
56.       /*
57.        * 实列化类 方法1
58.        */ 
59.       //String classPath = "com.whbs.bean.UserBean";  
60.       //Class cla = Test1.class.getClassLoader().loadClass(classPath);  
61.       //Object ob = cla.newInstance();  
62.       
63.       /*
64.        * 实列化类 方法2
65.        */ 
66.       UserBean bean = new UserBean(); 
67.       bean.setId(100); 
68.       bean.setAddress("武汉"); 
69.       
70.       //得到类对象  
71.       Class userCla = (Class) bean.getClass(); 
72.       
73.       /*
74.        * 得到类中的所有属性集合
75.        */ 
76.       Field[] fs = userCla.getDeclaredFields(); 
77.       for(int i = 0 ; i < fs.length; i++){ 
78.           Field f = fs[i]; 
79.           f.setAccessible(true); //设置些属性是可以访问的  
80.           Object val = f.get(bean);//得到此属性的值     
81.       
82.           System.out.println("name:"+f.getName()+"\t value = "+val); 
83.           
84.           String type = f.getType().toString();//得到此属性的类型  
85.           if (type.endsWith("String")) { 
86.              System.out.println(f.getType()+"\t是String"); 
87.              f.set(bean,"12") ;        //给属性设值  
88.           }else if(type.endsWith("int") || type.endsWith("Integer")){ 
89.              System.out.println(f.getType()+"\t是int"); 
90.              f.set(bean,12) ;       //给属性设值  
91.           }else{ 
92.              System.out.println(f.getType()+"\t"); 
93.           } 
94.           
95.       } 
96.       
97.       
98.       /*
99.        * 得到类中的方法
100.        */ 
101.       Method[] methods = userCla.getMethods(); 
102.       for(int i = 0; i < methods.length; i++){ 
103.           Method method = methods[i]; 
104.           if(method.getName().startsWith("get")){ 
105.              System.out.print("methodName:"+method.getName()+"\t"); 
106.              System.out.println("value:"+method.invoke(bean));//得到get 方法的值  
107.           } 
108.       } 
109.    } 
110.  
111.} 

猜你喜欢

转载自zhongdao.iteye.com/blog/2272281
今日推荐