public static void main(String args[]) { Class c = null; Package p = null; Constructor constructors[]; Field fields[]; Method methods[]; int i,j; try{ c = Class.forName("java.net.URLDecoder"); //输出的package的名称 System.out.println("package:"); p = c.getPackage(); if(p != null) { System.out.println(p.getName()); } // 输出class或interface的名称,及其修饰符(modifiers) System.out.println(); System.out.println("class:"); int mod = c.getModifiers(); System.out.print(Modifier.toString(mod)); //整个modifier if(Modifier.isInterface(mod)) { System.out.print(" "); //modifier内已经包含关键词 "interface" } else { System.out.print(" class "); //关键词 "class" } System.out.println(c.getName()); //输出构造函数 System.out.println(); System.out.println("constructors:"); constructors = c.getConstructors(); for(i=0; i<constructors.length; i++) { //System.out.print(constructors[i].toGenericString()); //上述语句输出:public java.util.LinkedList(java.util.Collection<? extends E>) //下面的语句合作输出:public java.util.LinkedList(java.util.Collection arg0) //这两种方法的输出结果大同小异 System.out.print(Modifier.toString(constructors[i].getModifiers())); System.out.print(" " + constructors[i].getName()); System.out.print("("); Class cx[] = constructors[i].getParameterTypes(); if(cx.length >= 1) { System.out.print(cx[0].getName()); System.out.print(" arg0"); } for(j=1; j<cx.length; j++) { System.out.print(", " + cx[j].getName()); System.out.print(" arg" + j); } System.out.println(")"); } //输出变量 System.out.println(); System.out.println("variables:"); fields = c.getDeclaredFields(); for(i=0; i<fields.length; i++) { System.out.print(Modifier.toString(fields[i].getModifiers())); System.out.print(" " + fields[i].getType().getName()); System.out.println(" " + fields[i].getName()); } //输出方法 System.out.println(); System.out.println("methods:"); methods = c.getDeclaredMethods(); for(i=0; i<methods.length; i++) { System.out.print(Modifier.toString(methods[i].getModifiers())); Class returnClass = methods[i].getReturnType(); System.out.print(" " + returnClass.getName() + " " + methods[i].getName() + "("); Class cx[] = methods[i].getParameterTypes(); if(cx.length >= 1) { System.out.print(cx[0].getName()); System.out.print(" arg0"); } for(j=1; j<cx.length; j++) { System.out.print(", " + cx[j].getName()); System.out.print(" arg" + j); } System.out.println(")"); } //输出父类 System.out.println(); System.out.println("super class:"); Class superClass = c.getSuperclass(); if(superClass != null) { System.out.println(superClass.getName()); } //输出实现的接口 System.out.println(); System.out.println("implemented interfaces:"); Class interfaces[] = c.getInterfaces(); for(i=0; i<interfaces.length; i++) { System.out.print(interfaces[i].getName() + ", "); } System.out.println(); //输出inner classes System.out.println(); System.out.println("inner classes:"); Class innerClasses[] = c.getDeclaredClasses(); for(Class innerClass : innerClasses) { System.out.print(innerClass.getName() + ", "); } System.out.println(); //输出outer classes System.out.println(); System.out.println("outer classes:"); Class outerClass = c.getDeclaringClass(); if(outerClass != null) System.out.println(outerClass.getName()); else System.out.println("null"); } catch(Exception e) { e.printStackTrace(); } }
运行结果如下图: