Code of learning reflection

xiaoxiao2021-03-06  68

Package test; import javax.swing. *;

Import java.lang.reflect.field; import java.lang.reflect.constructor; import java.lang.reflect.Modifier; import java.lang.reflect.Method;

PUBLIC CLASS Test {Public Test () {}

public static void printConstructors (Class c1) {Constructor [] constructors = c1.getDeclaredConstructors (); for (int i = 0; i

Class [] paramtypes = c.getParameterTypes (); for (int J = 0; j 0) system.out.print (","); system.out.print PARAMTYPES [J] .getname ());} system.out.println (");");}}

Public Static Void PrintMethods (Class C1) {Method [] methods = c1.getDeclaredMethods (); for (int i = 0; i

Class [] paramtypes = m.getParameterTypes ();

For (int J = 0; j 0) system.out.print (","); system.out.print (paramtypes [j] .getname ());

} System.out.println (");");}}

Public Static Void Printfields (class c1) {field [] fields = c1.getdeclaredfields ();

For (int i = 0; i 0) {name = args [0];

} else {name = JOPANE.SHOWINPUTDIALOG ("class name (eg java.util.date):");} try {class c1 = class.forname (name); class superc1 = c1.getsuperclass (); system.out. Println ("Class" Name); System.out.Println ("/ n {/ N"); PrintConstructors (C1); System.out.Println (); PrintMethods (C1); System.out.Println (); Printfields (C1); System.out.Println ("}");

} Catch (ClassNotFoundException E) {E.PrintStackTrace ();} system.exit (0);

}

转载请注明原文地址:https://www.9cbs.com/read-110498.html

New Post(0)