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

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

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

    積少成多

    垃圾堆

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      29 Posts :: 1 Stories :: 5 Comments :: 0 Trackbacks
    /**
     * 改類掩飾了如何通過java.bean的類去獲知bean中的屬性并調用相應的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 思無 閱讀(802) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 日韩版码免费福利视频| 青青草免费在线视频| 一本色道久久综合亚洲精品高清| 狠狠色香婷婷久久亚洲精品| 在线观看的免费网站| 亚洲国产系列一区二区三区| 在线看片免费不卡人成视频| 亚洲av无码不卡久久| 久草在视频免费福利| 亚洲午夜精品久久久久久app| 大地资源二在线观看免费高清| 亚洲精品久久无码av片俺去也 | 亚洲中文字幕久久精品无码APP | 色欲A∨无码蜜臀AV免费播| 亚洲AV无码第一区二区三区| 最好看的中文字幕2019免费| 亚洲人成电影青青在线播放| 成人看的午夜免费毛片| 羞羞漫画登录页面免费| 中文字幕亚洲一区| 一区二区免费视频| 亚洲精品无码aⅴ中文字幕蜜桃| 免费国产成人高清在线观看麻豆 | 亚洲日韩欧洲乱码AV夜夜摸| 久久成人a毛片免费观看网站| 亚洲人成网网址在线看| 免费观看美女裸体网站| 国产成人精品免费视频大全| 久久久无码精品亚洲日韩按摩| 无码视频免费一区二三区| 免费人成网站永久| 亚洲一区二区三区首页| 日日夜夜精品免费视频| 巨胸喷奶水www永久免费| 亚洲天堂一区二区三区四区| 免费a级毛片在线观看| 久久久久免费精品国产小说| 亚洲av日韩aⅴ无码色老头| 日本亚洲欧洲免费天堂午夜看片女人员 | 国产精品亚洲а∨无码播放不卡 | 日本19禁啪啪无遮挡免费动图|