package com.Mario.Classtest; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class classtest { @SuppressWarnings("rawtypes") public static void main(String[] args) { // TODO Auto-generated method stub // 打印類中 的詳細信息; try { Class cc = Class.forName("com.Mario.Classtest.person"); // 獲取構造函數 getConstructInfo(cc.getDeclaredConstructors()); // 獲取方法 getMethodsInfo(cc.getDeclaredMethods()); // 獲取內部類 getInerclassInfo(cc.getDeclaredClasses()); } catch (Exception e) { // TODO: handle exception System.out.println(e); } } public static void getConstructInfo( @SuppressWarnings("rawtypes") Constructor[] cons) { System.out.println("構造函數"); for (Constructor<?> con : cons) { // 打印修飾符 int mo = con.getModifiers(); System.out.print(Modifier.toString(mo)); // 打印構造函數名 System.out.print(" " + con.getName()); // 獲取參數; getParmsInfo(con.getParameterTypes()); System.out.println(); } } public static void getMethodsInfo(Method[] method) { for (Method mt : method) { int m = mt.getModifiers(); // 修飾符 System.out.print(Modifier.toString(m) + " "); // 獲取返回類型 System.out.print(mt.getReturnType()); System.out.print(" " + mt.getName()); getParmsInfo(mt.getParameterTypes()); } } public static void getParmsInfo(@SuppressWarnings("rawtypes") Class[] parm) { System.out.print(" ("); for (Class<?> c : parm) { System.out.print(c.getName() + " "); } System.out.print(")"); System.out.println(); } public static void getInerclassInfo( @SuppressWarnings("rawtypes") Class[] innerClass) { System.out.println("內部類"); for (@SuppressWarnings("rawtypes") Class c : innerClass) { System.out.println(c.getName() + "{"); getMethodsInfo(c.getDeclaredMethods()); System.out.println("}"); } } }