Java 反射機制

來源:互聯網
上載者:User

標籤:i++   ring   des   繼承   xtend   異常類   ext   cep   declared   


import java.lang.annotation.Annotation;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Modifier;/* * 測試反射機制,根據類名從而得到其中的資訊 */public class ClassLoadTest {    public static void main(String[] args) throws IllegalArgumentException, InvocationTargetException, ClassNotFoundException, InstantiationException, IllegalAccessException {        String className = "test.son";            Class getClass = Class.forName(className);            Object getObject = getClass.newInstance();            /*             * 獲得類的建構函式及其形參和異常             */            System.out.println("=======================================================");            Constructor[] constructList = getClass.getDeclaredConstructors();//            Constructor[] constructList = getClass.getConstructors();            for (int i = 0; i < constructList.length; i++) {                Constructor<?> constructor = constructList[i];                System.out.println("構造方法名稱" + i + ":" + constructor.getName());                System.out.println("所在類或介面:" + constructor.getDeclaringClass());                Class<?>[] constructParamList = constructor.getParameterTypes();                for (int j = 0; j < constructParamList.length; i++) {                    System.out.println("構造方法形參 " + i + " 的類型:" + constructParamList[i].getName());                }                Class<?>[] getExcepList = constructor.getExceptionTypes();                for (int j = 0; j < getExcepList.length; j++) {                    System.out.println("構造方法異常:" + j + "的類型:" + getExcepList[j].getName());                }                System.out.println("---------------------------");            }            /*             * 獲得對應Class的方法及其形參和異常(方法名、方法所在類或介面、形參類型、異常類型             */            System.out.println("=======================================================");            /*             * getDeclaredMethods獲得Class所表示的全部私人、預設、受保護和共有的方法,但不包括繼承地方法(             * 如果Super類和Sub類在同一個Java源檔案中進行編譯,顯示的結果會包括父類共有的方法)              * getMethodshuode 獲得Class所表示的類及其父類所有的公用方法             * getDeclaredConstructors和getConstructors也是一樣             */            // Method[] methods = c.getDeclaredMethods();            Method[] methods = getClass.getMethods();            for (Method m : methods) {                Annotation[] annotations = m.getAnnotations();                System.out.println("得到注釋"+annotations.toString());                System.out.println("方法名: " + m.getName());                System.out.println("該方法所在類或者介面:" + m.getDeclaringClass());                Class<?>[] pramaterList = m.getParameterTypes();                for (int i = 0; i < pramaterList.length; i++) {                    System.out.println("形參 " + i + " 的類型:" + pramaterList[i].getName());                }                Class<?>[] getExcepList = m.getExceptionTypes();                for (int i = 0; i < getExcepList.length; i++) {                    System.out.println("異常:" + i + "的類型:" + getExcepList[i].getName());                }//                m.invoke(getObject, null);//執行方法其中裡面涉及到可變參數                System.out.println("---------------------------");            }            /*             * 擷取對應Class的屬性(名稱、所在介面或類、類型、許可權修飾             */            System.out.println("=======================================================");            Field filedList[] = getClass.getDeclaredFields();            for (int i = 0; i < filedList.length; i++) {                Field f = filedList[i];                System.out.println("屬性" + i + ":" + f.getName());                System.out.println("所在介面或類:" + f.getDeclaringClass());                System.out.println("屬性類型:" + f.getType());                System.out.println("屬性修飾為:" + Modifier.toString(f.getModifiers()));                System.out.println("---------------------------");            }    }}

 





public class son extends father{        static {            System.out.println("(son) static code");        }            public son() {            System.out.println("(son)建構函式");        }                private int privateParameter_S;        public int publicParameter_S;        protected int protectedParameter_S;        int defaultParameter_S;                //注釋(son)調用公用的方法        public void PublicMethod_S(){            System.out.println("(son)調用公用的方法");        }        private  void PrivateMethod_S(){            System.out.println("(son)調用私用的方法");        }        protected void ProtectedMethod_S(){            System.out.println("(son)調用受保護的方法");        }        void DeSaultMethod_S(){            System.out.println("(son)調用預設的方法");        }    }        class father{        private int privateParameter_F;        public int publicParameter_F;        protected int protectedParameter_F;        int defaultParameter_F;                        public father() {            super();            System.out.println("(father)建構函式");        }        public void PublicMethod_F(){            System.out.println("(father)調用公用的方法");        }        private  void PrivateMethod_F(){            System.out.println("(father)調用私用的方法");        }        protected void ProtectedMethod_F(){            System.out.println("(father)調用受保護的方法");        }        void DefaultMethod_F(){            System.out.println("(father)調用預設的方法");        }    }





 

Java 反射機制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.