1 輸入一個類名:java.lang.String將打印出String類定義的結構,例如:java
public final class java.lang.String { public java.lang.String([B,int,int); public java.lang.String([B,java.nio.charset.Charset); public java.lang.String([B,java.lang.String); public java.lang.String([B,int,int,java.nio.charset.Charset); public java.lang.String([B,int,int,java.lang.String); java.lang.String([C,boolean); public java.lang.String(java.lang.StringBuilder); public java.lang.String(java.lang.StringBuffer); public java.lang.String([B); public java.lang.String([I,int,int); public java.lang.String(); public java.lang.String([C); public java.lang.String(java.lang.String); public java.lang.String([C,int,int); public java.lang.String([B,int); public java.lang.String([B,int,int,int); public boolean equals(java.lang.Object); public java.lang.String toString(); public int hashCode(); public int compareTo(java.lang.String); public volatile int compareTo(java.lang.Object); public int indexOf(java.lang.String,int); public int indexOf(java.lang.String); public int indexOf(int,int); public int indexOf(int); static int indexOf([C,int,int,[C,int,int,int); static int indexOf([C,int,int,java.lang.String,int); public static java.lang.String valueOf(int); public static java.lang.String valueOf(long); public static java.lang.String valueOf(float); public static java.lang.String valueOf(boolean); public static java.lang.String valueOf([C); public static java.lang.String valueOf([C,int,int); public static java.lang.String valueOf(java.lang.Object); public static java.lang.String valueOf(char); public static java.lang.String valueOf(double); public char charAt(int); private static void checkBounds([B,int,int); public int codePointAt(int); public int codePointBefore(int); public int codePointCount(int,int); public int compareToIgnoreCase(java.lang.String); public java.lang.String concat(java.lang.String); public boolean contains(java.lang.CharSequence); public boolean contentEquals(java.lang.CharSequence); public boolean contentEquals(java.lang.StringBuffer); public static java.lang.String copyValueOf([C); public static java.lang.String copyValueOf([C,int,int); public boolean endsWith(java.lang.String); public boolean equalsIgnoreCase(java.lang.String); public static transient java.lang.String format(java.util.Locale,java.lang.String,[Ljava.lang.Object;); public static transient java.lang.String format(java.lang.String,[Ljava.lang.Object;); public void getBytes(int,int,[B,int); public [B getBytes(java.nio.charset.Charset); public [B getBytes(java.lang.String); public [B getBytes(); public void getChars(int,int,[C,int); void getChars([C,int); private int indexOfSupplementary(int,int); public native java.lang.String intern(); public boolean isEmpty(); public static transient java.lang.String join(java.lang.CharSequence,[Ljava.lang.CharSequence;); public static java.lang.String join(java.lang.CharSequence,java.lang.Iterable); public int lastIndexOf(int); public int lastIndexOf(java.lang.String); static int lastIndexOf([C,int,int,java.lang.String,int); public int lastIndexOf(java.lang.String,int); public int lastIndexOf(int,int); static int lastIndexOf([C,int,int,[C,int,int,int); private int lastIndexOfSupplementary(int,int); public int length(); public boolean matches(java.lang.String); private boolean nonSyncContentEquals(java.lang.AbstractStringBuilder); public int offsetByCodePoints(int,int); public boolean regionMatches(int,java.lang.String,int,int); public boolean regionMatches(boolean,int,java.lang.String,int,int); public java.lang.String replace(char,char); public java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence); public java.lang.String replaceAll(java.lang.String,java.lang.String); public java.lang.String replaceFirst(java.lang.String,java.lang.String); public [Ljava.lang.String; split(java.lang.String); public [Ljava.lang.String; split(java.lang.String,int); public boolean startsWith(java.lang.String,int); public boolean startsWith(java.lang.String); public java.lang.CharSequence subSequence(int,int); public java.lang.String substring(int); public java.lang.String substring(int,int); public [C toCharArray(); public java.lang.String toLowerCase(java.util.Locale); public java.lang.String toLowerCase(); public java.lang.String toUpperCase(); public java.lang.String toUpperCase(java.util.Locale); public java.lang.String trim(); private final [C value; private int hash; private static final long serialVersionUID; private static final [Ljava.io.ObjectStreamField; serialPersistentFields; public static final java.util.Comparator CASE_INSENSITIVE_ORDER; }
主程序以下:ui
public static void main(String[] args) throws ClassNotFoundException { String name; if (args.length>0) { name=args[0]; } else { Scanner in=new Scanner(System.in); System.out.println("enter class name(e.g java.util.Date)"); name=in.next(); } try { Class c1=Class.forName(name); Class superClass=c1.getSuperclass(); //獲取類的修飾符 String modifiers=Modifier.toString(c1.getModifiers()); if (modifiers.length()>0) { System.out.print(modifiers+" "); } System.out.print("class "+c1.getName()); if (superClass!=null&&superClass!=Object.class) { System.out.print(" extends "+superClass.getName()); } //\n表示換行 System.out.print("\n{\n"); //打印構造器 printConstractors(c1); System.out.println(); //打印類的方法 printMethods(c1); System.out.println(); //打印域 printFields(c1); System.out.println("}"); } catch (Exception e) { // TODO: handle exception } }
private static void printFields(Class c1) { // TODO Auto-generated method stub Field[] fields= c1.getDeclaredFields(); for (Field field : fields) { Class type= field.getType(); String name=field.getName(); System.out.print(" "); String modifiers=Modifier.toString(field.getModifiers()); if (modifiers.length()>0) { System.out.print(modifiers+" "); } System.out.println(type.getName()+" "+name+";"); } } private static void printMethods(Class c1) { // TODO Auto-generated method stub //獲取當前類的全部方法 Method[] methods= c1.getDeclaredMethods(); for (Method m : methods) { Class returnType= m.getReturnType(); String methodName=m.getName(); String modifiers=Modifier.toString(m.getModifiers()); if (modifiers.length()>0) { System.out.print(" "+modifiers+" "); } System.out.print(returnType.getName()+" "+methodName+"("); //打印方法參數 Class[] paramTypes= m.getParameterTypes(); for (int i = 0; i < paramTypes.length; i++) { if (i>0) { System.out.print(","); } System.out.print(paramTypes[i].getName()); } System.out.println(");"); } } private static void printConstractors(Class c1) { // TODO Auto-generated method stub //獲得當前類的全部構造器,包括受保護的和私有的,不包含超類的 Constructor[] constructors= c1.getDeclaredConstructors(); for (Constructor constructor : constructors) { System.out.print(" "); String name=constructor.getName(); String modifiers=Modifier.toString(constructor.getModifiers()); if (modifiers.length()>0) { System.out.print(modifiers+" "); } System.out.print(name+"("); //打印構造器的參數 Class[] paramsTypes= constructor.getParameterTypes(); for (int i = 0; i < paramsTypes.length; i++) { if (i>0) { System.out.print(","); } System.out.print(paramsTypes[i].getName()); } System.out.println(");"); } }