<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 思無 閱讀(803) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 亚洲伦理中文字幕| 一级有奶水毛片免费看| 色在线亚洲视频www| 222www在线观看免费| 免费黄色小视频网站| 亚洲精品成人无限看| 亚洲免费网站观看视频| 少妇人妻偷人精品免费视频| 免费观看四虎精品国产永久| 亚洲大片在线观看| 亚洲国产精品99久久久久久| 最近2018中文字幕免费视频| 亚洲啪啪AV无码片| 一区二区三区免费视频观看 | 亚洲综合无码一区二区痴汉| 国产精品白浆在线观看免费| 亚洲精品视频免费| 亚洲色www永久网站| 日韩激情淫片免费看| 亚洲大成色www永久网址| 最近中文字幕大全免费视频 | 日韩吃奶摸下AA片免费观看| 亚洲另类激情综合偷自拍| 精品无码一级毛片免费视频观看| 亚洲真人无码永久在线| 久久久久久亚洲精品无码| 国产卡一卡二卡三免费入口| 亚洲综合成人网在线观看| 好紧我太爽了视频免费国产| 中文字幕在线亚洲精品| 久久精品无码专区免费东京热 | 亚洲精品NV久久久久久久久久| 人妻免费一区二区三区最新| 亚洲欧洲久久久精品| 大地资源网高清在线观看免费 | 在线成人精品国产区免费| 亚洲a视频在线观看| 动漫黄网站免费永久在线观看 | 久久免费精品一区二区| 国产成人精品日本亚洲网址| 亚洲精品无码激情AV|