<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    積少成多

    垃圾堆

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      29 Posts :: 1 Stories :: 5 Comments :: 0 Trackbacks
    /**
     * 改類掩飾了如何通過java.bean的類去獲知bean中的屬性并調(diào)用相應的set get方法
     
    */
    import java.beans.*;
    import java.lang.reflect.Method;
    public class JavaBeanInvoke {
        
    public static void main(String args[])throws Exception{
            test1();
        }
        
        
    public static void test1()throws Exception{
            Person person 
    = Person.class.newInstance();
            
            BeanInfo beaninfo 
    = Introspector.getBeanInfo(Person.class);
            PropertyDescriptor[] porpertydescriptors 
    = beaninfo.getPropertyDescriptors();
            
    for(PropertyDescriptor pd:porpertydescriptors){
                System.out.println(pd.getName());
                
    if(pd.getName().endsWith("name")){
                    Method setMethod
    =pd.getWriteMethod();
                    Method getMethod
    =pd.getReadMethod();
                    setMethod.invoke(person,
    "ShenZiping");
                    System.out.println(getMethod.invoke(person));
                    
    break;
                }
            }
        }
        
        
    public static void test2() throws Exception{
            Person person 
    = Person.class.newInstance();
            PropertyDescriptor pd 
    = new PropertyDescriptor("age",Person.class);
            Method setMethod 
    = pd.getWriteMethod();
            Method getMethod 
    = pd.getReadMethod();
            setMethod.invoke(person, 
    56);
            System.out.println(getMethod.invoke(person));
        }
    }

    /**
     * 代碼舉例了beanUtil包的普遍用法,需要apache的logging包和beanUtils包
     
    */
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;

    import org.apache.commons.beanutils.*;
    public class BeanUtilsInvoke {
        
    public static void main(String args[])throws Exception{
            test1();
            test2();
            test3();
            test4();
            myConvertTest();
        }
        
    public static void test1()throws Exception{
            Person person 
    = new Person();
            BeanUtils.copyProperty(person, 
    "name""ShenZiping");
            System.out.println(
    "test1"+person.getName());
        }
        
    public static void test2() throws Exception{
            Person person 
    = new Person();
            Map
    <String, String> map = new HashMap<String, String>();
            map.put(
    "name""ShenZiping");
            map.put(
    "age""65");
            BeanUtils.populate(person, map);
            System.out.println(
    "test2"+person.getAge());
            System.out.println(
    "test2"+person.getName());
        }
        
        
    public static void test3() throws Exception{
            Person p1 
    = new Person();
            Person p2 
    = new Person();
            p1.setAge(
    98);
            p1.setName(
    "ShenZiping");
            BeanUtils.copyProperties(p2, p1);
            System.out.println(
    "test3"+p2.getAge());
            System.out.println(
    "test3"+p2.getName());
        }
        
        
    public static void test4() throws Exception{
            Person person 
    = new Person();
            Man man 
    = new Man();
            person.setName(
    "ShenZiping");
            BeanUtils.copyProperties(man, person);
            System.out.println(
    "test4"+man.getName());
        }
        
        
    public static void myConvertTest(){
            ConvertUtils.register(
    new Converter(){
                
    public Object convert(Class clazz, Object value){
                    
    if(clazz==Date.class){
                        SimpleDateFormat dateFormat 
    = new SimpleDateFormat("yyyy-MM-dd");
                        
    try{
                            
    return dateFormat.parse((String)value);
                        }
    catch(ParseException e){
                            
    throw new RuntimeException("invalid format");
                        }
                    }
                    
    return null;
                }
            }, Date.
    class);
            Date date 
    = (Date)ConvertUtils.convert("2010-01-15",Date.class);
            System.out.println(
    "myConvertTest"+date);
        }
    }
    posted on 2011-06-13 12:38 思無 閱讀(808) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 国产99视频精品免费专区| 国产无遮挡无码视频免费软件| 成全视频在线观看免费高清动漫视频下载| 亚洲国产成人超福利久久精品| 国产jizzjizz免费看jizz| 在线观看免费黄网站| 亚洲午夜精品一区二区公牛电影院| 午夜网站免费版在线观看| 久久精品成人免费国产片小草 | 国产AV无码专区亚洲Av| 在线观看永久免费| 美女视频黄频a免费观看| 亚洲丁香色婷婷综合欲色啪| 在线观看无码的免费网站| 91视频免费网站| 伊人久久五月丁香综合中文亚洲 | 女人18毛片水最多免费观看| 一个人免费观看日本www视频| 亚洲精品在线免费观看视频| 亚洲第一黄片大全| 一个人免费高清在线观看| a色毛片免费视频| 亚洲av无码一区二区三区在线播放| 亚洲成AV人片在线观看无 | 亚洲AV无码精品蜜桃| 中文字幕精品亚洲无线码一区| 99久久这里只精品国产免费| 国产在线观看免费av站| 精品久久久久久亚洲综合网| 亚洲剧场午夜在线观看| 亚洲精品乱码久久久久久自慰| 日韩在线视频免费看| 久草视频在线免费| 青青青国产手机频在线免费观看| 粉色视频在线观看www免费| 精品久久久久久亚洲精品| 久久国产亚洲高清观看| 亚洲精品乱码久久久久久自慰| 亚洲精品NV久久久久久久久久| 浮力影院第一页小视频国产在线观看免费 | 无码区日韩特区永久免费系列 |