1 Packageclasstest;2 3 ImportJava.lang.reflect.Constructor;4 ImportJava.lang.reflect.Field;5 ImportJava.lang.reflect.Method;6 7 /**8 * Print class information, including class constructors, member functions, member variables9 * @authorWangTen * One */ A Public classClassutil { - - /** the * Gets the information of the member method of the object - * - * @paramobj - */ + Public Static voidprintclassmethodmessage (Object obj) { - //to get information about a class, you first get the class type of the class, and the object C of which subclass is passed is the class type of the subclass +Class C =Obj.getclass (); A //gets the name of the class atSystem.out.println ("The name of the class is:" +c.getname ()); - /* - * Method Class, methods object A member method is an object - * The GetMethods () method gets all the public functions, including those inherited by the parent class . - * Getdeclaredmethods () gets all the methods that the class declares itself, without asking access rights - */ in //c.getdeclaredmethods () -Method[] ms =c.getmethods (); to for(inti = 0; i < ms.length; i++) { + //gets the class type of the return value type of the method -Class ReturnType =Ms[i].getreturntype (); theSystem.out.print (Returntype.getname () + ""); * //get the name of the method $System.out.print (Ms[i].getname () + "(");Panax Notoginseng //gets the parameter type---> Gets the class type of the type of the parameter list -class[] Paramtypes =ms[i].getparametertypes (); the for(Class class1:paramtypes) { +System.out.print (Class1.getname () + ","); A } theSystem.out.println (")"); + } - } $ $ /** - * Get information about the member variables of an object - * the * @paramobj - */Wuyi Public Static voidprintfieldmessage (Object obj) { theClass C =Obj.getclass (); - /* Wu * Member variables are also objects Java.lang.reflect.Field Field class encapsulates operations on member variables - * The GetFields () method gets the information for all public member variables About * Getdeclaredfields Gets the information about the member variables declared by the class itself $ */ - //field[] fs = C.getfields (); -field[] fs =c.getdeclaredfields (); - for(Field field:fs) { A //the class type that gets the type of the member variable +Class FieldType =Field.gettype (); theString TypeName =fieldtype.getname (); - //get the name of the member variable $String FieldName =field.getname (); theSystem.out.println (TypeName + "" +fieldName); the } the } the - /** in * Print information about the constructor of an object the * the * @paramobj About */ the Public Static voidprintconmessage (Object obj) { theClass C =Obj.getclass (); the /* + * Constructors are also object Java.lang. Information in constructor that encapsulates the constructor - * GetConstructors Get all the public constructors getdeclaredconstructors get all the constructors the */Bayi //constructor[] cs = c.getconstructors (); theConstructor[] cs =c.getdeclaredconstructors (); the for(Constructor constructor:cs) { -System.out.print (Constructor.getname () + "("); - //gets the argument list of the constructor---> Gets the class type of the argument list theclass[] Paramtypes =constructor.getparametertypes (); the for(Class class1:paramtypes) { theSystem.out.print (Class1.getname () + ","); the } -System.out.println (")"); the } the } the}
Traversing member methods, member variables, constructors through Java objects