標籤: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 反射機制