Package Liangxi;
Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
Public class ClassDemo3 {
//print the information for the class, including the member function of the class, the member variable
//obj The information that the object belongs to
public static void Printclassmethodmessage (Object obj) {
//To get the information of the class first to get the class type of the Class
Class C = Obj.getclass ();//The object that is passed the subclass C is the class type of the subclass
//Gets the name of the class
System.out.println ("The name of the class is:" +c.getname ());
Method[]ms = C.getmethods ();//c.getdeclaredmethod ()
//method class, Method object
//A member method is an object
// The GetMethod () method gets all of the public functions, including the inherited
//getdeclaredmethod () of the parent class, which gets all the methods that the class declares itself, without asking for access permissions
for (int i = 0; i< ms.length;i++) {
//Gets the class type of the return value type of the method
class ReturnType = Ms[i].getreturntype ();
System.out.print (Ms[i].getname ());
//Get the name of the method
System.out.print (Ms[i].getname () + "(");
Get the parameter type--Get the class type of the parameter list type
class[] paramtypes = Ms[i].getparametertypes ();
for (Class class1:paramtypes) {
System.out.print (class1.getname () + ",");
}
System.out.println (")");
Printfieldmessage (c);
}
}
public static void Printfieldmessage (Object obj) {
Class C =obj.getclass ();
The
//member variable is also an object
//java.lang.reflect.field
//field class encapsulates the action on member variables
//getfields () method gets information about the member variable of the public so
//getdeclaredfields Gets the information for the member variable declared by the class
//field[]fs=c.getfields ();
field[] fs = C.getdeclaredfields ();
for (Field field:fs) {
//Gets the class type of the member variable of type
class TypeName = Field.gettype ();
String fieldName = Field.getname ();
System.out.println (typename+ "" +fieldname);
}
}
public static void Printconmessage (Object obj) {
Class c = obj.getclass ();
The information that encapsulates the constructor in Java.lang.Constructor
//constructor is also object
//constructor[] cs = c.getconstructors ();
Constructor[]cs = C.getdeclaredconstructors ();
for (Constructor constructor:cs) {
System.out.print (constructor.getname () + "(");
Gets the argument list of the constructor
Class[]paramtypes = Constructor.getparametertypes ();
for (Class class1:paramtypes) {
System.out.print (class1.getname () + ",");
}
System.out.println (")");
}
}
}
Test
Package Liangxi;
Import Java.lang.reflect.Field;
public class ClassDemo33 {
public static void Main (string[] args) {
String s = "Hello";
Classdemo3.printclassmethodmessage (s);
Integer n1 = 1;
Classdemo3.printclassmethodmessage (N1);
The class class is used to get the method information returned so the integer class information
Member variables are also objects
Java.lang.reflect.Field
The field class encapsulates the operation on member variables
}
}
The output is all the methods and information reflection out.
Access to methods and information through reflection