亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1700 | 回復(fù): 0
打印 上一主題 下一主題

java反射的例子 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2009-06-15 09:55 |只看該作者 |倒序?yàn)g覽

Reflection 是Java被視為動(dòng)態(tài)(或準(zhǔn)動(dòng)態(tài))語言的一個(gè)關(guān)鍵性質(zhì)。這個(gè)機(jī)制允許程序在運(yùn)行時(shí)透過Reflection APIs取得任何一個(gè)已知名稱的class的內(nèi)部信息,包括其modifiers(諸如public, static 等等)、superclass(例如Object)、實(shí)現(xiàn)之interfaces(例如Cloneable),也包括fields和methods的所有信息,并可于運(yùn)行時(shí)改變fields內(nèi)容或喚起methods。
個(gè)人理解就是在運(yùn)行時(shí)可以得到某個(gè)對(duì)象的所有信息,包括方法,類型,屬性,方法參數(shù),方法返回值以及可以調(diào)用該類的所有方法。
下面是兩個(gè)例子:
Java代碼
[/url]
  • package cn.test;   
  •   
  •   
  • import java.lang.reflect.Array;   
  • import java.lang.reflect.Constructor;   
  • import java.lang.reflect.Field;   
  • import java.lang.reflect.Method;   
  •   
  •   
  • /**
  • * Java Reflection Cookbook
  • *
  • * @author Michael Lee
  • * @since 2006-8-23
  • * @version 0.1a
  • */  
  •   
  • public class TestReflection {   
  •     /**
  •      * 得到某個(gè)對(duì)象的公共屬性
  •      *
  •      * @param owner, fieldName
  •      * @return 該屬性對(duì)象
  •      * @throws Exception
  •      *
  •      */  
  •     public Object getProperty(Object owner, String fieldName) throws Exception {   
  •         Class ownerClass = owner.getClass();   
  •   
  •         Field field = ownerClass.getField(fieldName);   
  •   
  •         Object property = field.get(owner);   
  •   
  •         return property;   
  •     }   
  •   
  •     /**
  •      * 得到某類的靜態(tài)公共屬性
  •      *
  •      * @param className   類名
  •      * @param fieldName   屬性名
  •      * @return 該屬性對(duì)象
  •      * @throws Exception
  •      */  
  •     public Object getStaticProperty(String className, String fieldName)   
  •             throws Exception {   
  •         Class ownerClass = Class.forName(className);   
  •   
  •         Field field = ownerClass.getField(fieldName);   
  •   
  •         Object property = field.get(ownerClass);   
  •   
  •         return property;   
  •     }   
  •   
  •   
  •     /**
  •      * 執(zhí)行某對(duì)象方法
  •      *
  •      * @param owner
  •      *            對(duì)象
  •      * @param methodName
  •      *            方法名
  •      * @param args
  •      *            參數(shù)
  •      * @return 方法返回值
  •      * @throws Exception
  •      */  
  •     public Object invokeMethod(Object owner, String methodName, Object[] args)   
  •             throws Exception {   
  •   
  •         Class ownerClass = owner.getClass();   
  •   
  •         Class[] argsClass = new Class[args.length];   
  •   
  •         for (int i = 0, j = args.length; i
  •             argsClass = args.getClass();   
  •         }   
  •   
  •         Method method = ownerClass.getMethod(methodName, argsClass);   
  •   
  •         return method.invoke(owner, args);   
  •     }   
  •   
  •   
  •       /**
  •      * 執(zhí)行某類的靜態(tài)方法
  •      *
  •      * @param className
  •      *            類名
  •      * @param methodName
  •      *            方法名
  •      * @param args
  •      *            參數(shù)數(shù)組
  •      * @return 執(zhí)行方法返回的結(jié)果
  •      * @throws Exception
  •      */  
  •     public Object invokeStaticMethod(String className, String methodName,   
  •             Object[] args) throws Exception {   
  •         Class ownerClass = Class.forName(className);   
  •   
  •         Class[] argsClass = new Class[args.length];   
  •   
  •         for (int i = 0, j = args.length; i
  •             argsClass = args.getClass();   
  •         }   
  •   
  •         Method method = ownerClass.getMethod(methodName, argsClass);   
  •   
  •         return method.invoke(null, args);   
  •     }   
  •   
  •   
  •   
  •     /**
  •      * 新建實(shí)例
  •      *
  •      * @param className
  •      *            類名
  •      * @param args
  •      *            構(gòu)造函數(shù)的參數(shù)
  •      * @return 新建的實(shí)例
  •      * @throws Exception
  •      */  
  •     public Object newInstance(String className, Object[] args) throws Exception {   
  •         Class newoneClass = Class.forName(className);   
  •   
  •         Class[] argsClass = new Class[args.length];   
  •   
  •         for (int i = 0, j = args.length; i
  •             argsClass = args.getClass();   
  •         }   
  •   
  •         Constructor cons = newoneClass.getConstructor(argsClass);   
  •   
  •         return cons.newInstance(args);   
  •   
  •     }   
  •   
  •   
  •       
  •     /**
  •      * 是不是某個(gè)類的實(shí)例
  •      * @param obj 實(shí)例
  •      * @param cls 類
  •      * @return 如果 obj 是此類的實(shí)例,則返回 true
  •      */  
  •     public boolean isInstance(Object obj, Class cls) {   
  •         return cls.isInstance(obj);   
  •     }   
  •       
  •     /**
  •      * 得到數(shù)組中的某個(gè)元素
  •      * @param array 數(shù)組
  •      * @param index 索引
  •      * @return 返回指定數(shù)組對(duì)象中索引組件的值
  •      */  
  •     public Object getByArray(Object array, int index) {   
  •         return Array.get(array,index);   
  •     }   
  • }  package cn.test;
    import java.lang.reflect.Array;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    /**
    * Java Reflection Cookbook
    *
    * @author Michael Lee
    * @since 2006-8-23
    * @version 0.1a
    */
    public class TestReflection {
        /**
         * 得到某個(gè)對(duì)象的公共屬性
         *
         * @param owner, fieldName
         * @return 該屬性對(duì)象
         * @throws Exception
         *
         */
        public Object getProperty(Object owner, String fieldName) throws Exception {
            Class ownerClass = owner.getClass();
            Field field = ownerClass.getField(fieldName);
            Object property = field.get(owner);
            return property;
        }
        /**
         * 得到某類的靜態(tài)公共屬性
         *
         * @param className   類名
         * @param fieldName   屬性名
         * @return 該屬性對(duì)象
         * @throws Exception
         */
        public Object getStaticProperty(String className, String fieldName)
                throws Exception {
            Class ownerClass = Class.forName(className);
            Field field = ownerClass.getField(fieldName);
            Object property = field.get(ownerClass);
            return property;
        }
        /**
         * 執(zhí)行某對(duì)象方法
         *
         * @param owner
         *            對(duì)象
         * @param methodName
         *            方法名
         * @param args
         *            參數(shù)
         * @return 方法返回值
         * @throws Exception
         */
        public Object invokeMethod(Object owner, String methodName, Object[] args)
                throws Exception {
            Class ownerClass = owner.getClass();
            Class[] argsClass = new Class[args.length];
            for (int i = 0, j = args.length; i
    Java代碼
    [url=http://canofy.javaeye.com/blog/359565#]

  • package cn.test;   
  •   
  • import java.lang.reflect.Field;   
  • import java.lang.reflect.InvocationTargetException;   
  • import java.lang.reflect.Method;   
  • import java.util.ArrayList;   
  •   
  • import cn.IpUtils.IpBean;   
  •   
  • public class TestObject {   
  •       
  •     /**
  •      * 設(shè)置屬性值
  •      * @param list
  •      * @param cla
  •      * @return
  •      */  
  •     public ArrayList array2bean(ArrayList list, Class cla) {   
  •         ArrayList result = new ArrayList();   
  •         int filed_len = cla.getDeclaredFields().length;   
  •         System.out.println(":"+cla.getDeclaredFields().length);   
  •         for (int i = 0; i
  •             Object[] o = (Object[]) list.get(i);   
  •             int length = filed_len
  •             try {   
  •                 result.add(cla.newInstance());   
  •                 for (int j = 0; j
  •                     Method m = null;   
  •                     String mName =cla.getDeclaredFields()[j].getName();   
  •                     mName = mName.substring(0, 1).toUpperCase()+ mName.substring(1);   
  •                     mName = "set" + mName;   
  •                     m = cla.getMethod(mName, new Class[] { String.class });   
  •                     //調(diào)用設(shè)置的方法,給屬性賦值   
  •                     m.invoke(result.get(i), new Object[] { o[j] == null ? "": o[j].toString() });   
  •                 }   
  •             } catch (Exception e) {   
  •                 e.printStackTrace();   
  •             }   
  •         }   
  •         return result;   
  •     }   
  •       
  •     /**
  •      * 獲取get的取值
  •      * @param obj
  •      * @return
  •      * @throws Exception
  •      */  
  •     public String getObjectToString(Object obj) throws Exception{   
  •         Class cla=obj.getClass();   
  •         Method[] ma=cla.getDeclaredMethods();//獲取所有聲明的方法數(shù)組   
  •         Method method=null;   
  •         String methodName=null;   
  •         Object returnValue=null;   
  •         for(int i=0;i
  •             method=ma;   
  •             methodName=method.getName();//方法名   
  •             if(methodName.indexOf("get")==0){//以get開始的方法,排除set方法   
  •                 returnValue=method.invoke(obj, null);//調(diào)用方法,相當(dāng)于執(zhí)行g(shù)et方法得到的結(jié)果,這里返回的是一個(gè)對(duì)象   
  •                 System.out.print(methodName+"::");   
  •                 System.out.println(returnValue==null?"":returnValue.toString());   
  •             }   
  •         }         
  •         return "";   
  •     }   
  •   
  •     /**
  •      * 獲取對(duì)象的屬性值,含有g(shù)et方法的
  •      * @param obj
  •      * @return
  •      * @throws NoSuchMethodException  
  •      * @throws SecurityException  
  •      * @throws InvocationTargetException  
  •      * @throws IllegalAccessException  
  •      * @throws IllegalArgumentException  
  •      */  
  •     public String getAttributeValue(Object obj) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{   
  •         Class cla=obj.getClass();   
  •         Field[] fds=cla.getDeclaredFields();   
  •         Field field=null;   
  •         String fieldName=null;   
  •         String methodName=null;   
  •         Method method=null;   
  •         Object returnValue=null;   
  •         for(int i=0;i
  •             field=fds;   
  •             fieldName=field.getName();   
  •             methodName="get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1);               
  •             method=cla.getDeclaredMethod(methodName, null);   
  •             returnValue=method.invoke(obj, null);//調(diào)用方法,相當(dāng)于執(zhí)行g(shù)et方法得到的結(jié)果,這里返回的是一個(gè)對(duì)象   
  •             System.out.print(methodName+"::");   
  •             System.out.println(returnValue==null?"":returnValue.toString());   
  •         }   
  •            
  •         return "";   
  •     }   
  •       
  •     /**
  •      * @param args
  •      * @throws Exception  
  •      */  
  •     public static void main(String[] args) throws Exception {   
  • //      IpBean.class   
  •         TestObject to=new TestObject();   
  •         IpBean ib=new IpBean();   
  •         ib.setFrom("from1");   
  •         ib.setPosition("position1");   
  •         ib.setTo("to1");   
  •         ib.setId(10);   
  •         to.getObjectToString(ib);   
  • //      to.getAttributeValue(ib);   
  •     }   
  •   
  • }  package cn.test;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import cn.IpUtils.IpBean;
    public class TestObject {
           
            /**
             * 設(shè)置屬性值
             * @param list
             * @param cla
             * @return
             */
            public ArrayList array2bean(ArrayList list, Class cla) {
                    ArrayList result = new ArrayList();
                    int filed_len = cla.getDeclaredFields().length;
                    System.out.println(":"+cla.getDeclaredFields().length);
                    for (int i = 0; i
    Java代碼
    [/url]
  • package cn.IpUtils;   
  •   
  • public class IpBean {   
  •     private String from; // IP段起始   
  •     private String to; // IP結(jié)束   
  •     private String position; // 地理名稱       
  •     private int id = 0;   
  •   
  •     public String getFrom() {   
  •         return from;   
  •     }   
  •   
  •     public void setFrom(String from) {   
  •         this.from = from;   
  •     }   
  •   
  •     public String getTo() {   
  •         return to;   
  •     }   
  •   
  •     public void setTo(String to) {   
  •         this.to = to;   
  •     }   
  •   
  •     public String getPosition() {   
  •         return position;   
  •     }   
  •   
  •     public void setPosition(String position) {   
  •         this.position = position;   
  •     }   
  •   
  •     public int getId() {   
  •         return id;   
  •     }   
  •   
  •     public void setId(int id) {   
  •         this.id = id;   
  •     }   
  •   
  •       
  • }  package cn.IpUtils;
    public class IpBean {
            private String from; // IP段起始
        private String to; // IP結(jié)束
        private String position; // 地理名稱   
        private int id = 0;
            public String getFrom() {
                    return from;
            }
            public void setFrom(String from) {
                    this.from = from;
            }
            public String getTo() {
                    return to;
            }
            public void setTo(String to) {
                    this.to = to;
            }
            public String getPosition() {
                    return position;
            }
            public void setPosition(String position) {
                    this.position = position;
            }
            public int getId() {
                    return id;
            }
            public void setId(int id) {
                    this.id = id;
            }
           
    }


    本文來自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):[url]http://blog.chinaunix.net/u1/57965/showart_1964357.html
  • 您需要登錄后才可以回帖 登錄 | 注冊(cè)

    本版積分規(guī)則 發(fā)表回復(fù)

      

    北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年舉報(bào)專區(qū)
    中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
    感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP