JAVA中所有類的祖先類都是object類,object類有一個很暴力的方法 getClass 例子,通過他得到子類的類名:getSimpleName
public?class?Test11A?????{ ?????public?void???check(Obj?j)?{? ?????????System.out.println(j.getClass().getSimpleName()); ????} ????public?static?void?main(String[]?args)?{ ????????//?TODO?自動生成的方法存根 ????????Test11A?test11a?=?new?Test11A(); ????????Obj?obj?=?new?Sub1();? ????????Obj?obj2?=?new?Sub2();? ????????test11a.check(obj); ????????test11a.check(obj2);?? ????} }
輸出: Sub1 Sub2
getName:
com.java.Obj.Sub1 com.java.Obj.Sub2
getDeclaredFields:獲得成員變量數(shù)組
public?class?Sub1?extends?Obj?{ ????private??int?x; ????private??int?y; ????public?String?string; ????public??void?Test() ????{ ????????System.out.println("Sub1?Test"); ????}
public?class?Test11A?????{ ?????public?void???check(Obj?j)?{? ?????????System.out.println(j.getClass().getClassLoader()); ????} ?????public?void???check2(Obj?j)?{? ????????????//?取得本類的全部屬性 ?????????Class?mclass?=null; ?????????mclass=?j.getClass(); ????????Field[]?fields=?mclass.getDeclaredFields(); ????????for?(int?i?=?0;?i?<?fields.length;?i++)?{ ????????????Field?field?=?fields[i]; ????????????Class??curClass=field.getType();? ????????????System.out.println(field.getModifiers()+","+curClass.getTypeName()+","+field.getName()); ????????} ????} ????public?static?void?main(String[]?args)?{ ????????//?TODO?自動生成的方法存根 ????????Test11A?test11a?=?new?Test11A(); ????????Obj?obj?=?new?Sub1();? ????????Obj?obj2?=?new?Sub2();? ????????test11a.check2(obj); ????????test11a.check2(obj2); ????} }
輸出: 2,int,x 2,int,y 1,java.lang.String,string
訪問修飾符是字符串,用java函數(shù)轉換:
public?void???check2(Obj?j)?{? ????????????//?取得本類的全部屬性 ?????????Class?mclass?=null; ?????????mclass=?j.getClass(); ????????Field[]?fields=?mclass.getDeclaredFields(); ????????for?(int?i?=?0;?i?<?fields.length;?i++)?{ ????????????Field?field?=?fields[i]; ?????????????Class??curClass=field.getType(); ????????????System.out.println(Modifier.toString((field.getModifiers()))+","+curClass.getTypeName()+","+field.getName()); ????????} ????}
輸出: private,int,x private,int,y public,java.lang.String,string
public?void?check2(Obj?j)?{ ????????//?取得本類的全部屬性 ????????Class?mclass?=?null; ????????mclass?=?j.getClass(); ????????Field[]?fields?=?mclass.getDeclaredFields(); ????????for?(int?i?=?0;?i?<?fields.length;?i++)?{ ????????????Field?field?=?fields[i]; ????????????Class?curClass?=?field.getType(); ????????????System.out.println( ????????????????????Modifier.toString((field.getModifiers()))?+?","?+?curClass.getTypeName()?+?","?+?field.getName()); ????????} ????}
public?void?check3(Obj?j)?{ ????//?取得本類的全部方法 ????Class?mclass?=?null; ????mclass?=?j.getClass(); ????Method[]?marray?=?mclass.getDeclaredMethods(); ????for?(int?i?=?0;?i?<?marray.length;?i++)?{ ????????Method?field?=?marray[i]; ?????????java.lang.reflect.Parameter[]?parameters=?field.getParameters(); ?????????for?(int?k?=?0;?k?<?parameters.length;?k++)?{ ????????????java.lang.reflect.Parameter?parameter?=?parameters[k]; ????????????System.out.println(parameter.getType().getName()); ????????} ?????????System.out.println(","+Modifier.toString(field.getModifiers())?+?"?"?+?field.getReturnType().getTypeName()?+?":" ????????????????+?field.getName()); ????} }
????public?void?check4(Obj?j)?{ ????????//?根據(jù)字符串調用對象方法 ????????Class?mclass?=?null; ????????mclass?=?j.getClass(); ????????try?{ ????????????Method?method?=?mclass.getDeclaredMethod("Test2",String.class,int.class); ????????????method.invoke(j,"hi",555); ????????}?catch?(NoSuchMethodException?|?SecurityException?|?IllegalAccessException?|?IllegalArgumentException ????????????????|?InvocationTargetException?e)?{ ????????????//?TODO?自動生成的?catch?塊 ????????????e.printStackTrace(); ????????} ????}
?void??check5(?)?throws?InstantiationException,?IllegalAccessException ????{ ?????????//根據(jù)類名生成對象實例 ?????????try?{ ????????????Class?mClass?=?Class.forName("com.java.Obj.Sub1"); ????????????????try{ ???????????????????Object?obj?=mClass.newInstance(); ????????????????????Method??fucMethod=?mClass.getMethod("Test2",?String.class,int.class); ????????????????????fucMethod.invoke(obj,?"hello?",123); ????????????????}catch(Exception?e){ ????????????????????e.printStackTrace(); ????????????????} ????????}?catch?(ClassNotFoundException?e)?{ ????????????//?TODO?自動生成的?catch?塊 ????????????e.printStackTrace(); ????????} ????}