Java使用反射机制使用get set 方法

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 
 */

/** 
 * @ClassName: ReflectTest 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author zhoushun 
 * @date 2013年11月26日 下午5:11:35 
 *  
 */
public class ReflectTest {
	public static void main() throws Exception{
		ReflectTest bo = new ReflectTest();
		Field[] fields = bo.getClass().getDeclaredFields();  
       // 写数据  
      for(Field ff : fields) {  
    	  String name = ff.getName();    //获取属性的名字  
    	  String type = ff.getGenericType().toString();    //获取属性的类型
          ff.setAccessible(true);  
       PropertyDescriptor pd = new PropertyDescriptor(ff.getName(), bo.getClass());  
       Method rM = pd.getReadMethod();//获得读方法  
       Object num = (Object) rM.invoke(bo);  
       if(num == null) {  
           Method wM = pd.getWriteMethod();//获得写方法  
           if(type.equals("class java.lang.Integer")){       
              
           }  
           if(type.equals("class java.lang.Short")){       
              
           }         
           if(type.equals("class java.lang.Double")){       
              
           }                    
           if(type.equals("class java.lang.Boolean")){  
              
           }  
           if(type.equals("class java.util.Date")){  
             
           }   
           wM.invoke(bo, "1");//因为知道是int类型的属性,所以传个int过去就是了。。实际情况中需要判断下他的参数类型  
       }  
      }  
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值