The following code is a tool class
Package com.imooc.reflect;
Import Java.lang.reflect.Method; public class Classutil {public static void Printclassmessage (Object obj) {//To get information about the class, first get the class type class C = Obj.getcl
Ass ();//Pass the object of which subclass, C is the class type of the subclass//Get the class name System.out.println ("Class name is:" +c.getname ()); * * Methods class, Method Object * A member method is a Methods object * GetMethods () method gets all the public functions, including the inherited from the parent class * Getdeclaredmethods () gets all
This class declares its own method, the location of access rights * * * method[] methods = C.getmethods ();
for (int i=0;i<methods.length;i++) {//the class type class ReturnType = Methods[i].getreturntype () of the return value type of the method.
System.out.print (Returntype.getname () + "");
Get the name of the method System.out.print (Methods[i].getname () + "(");
Get the parameter type--"The Type de class type of the argument list" class[] Paramtype = Methods[i].getparametertypes ();
for (Class class1:paramtype) {System.out.print (Class1.getname () + ",");
} System.out.println (")"); The name of the//output://class is: java.lang.String//boolean equals (Java.lang.Object,)//java.lang.string toString ()//int has HCode ()//.....
The following code is a test class:
Package com.imooc.reflect;
public class Testclassutil {public
static void Main (string[] args) {
string string = "Hello";
Classutil.printclassmessage (string);
Integer n1 = 1;
Classutil.printclassmessage (n1);
}
Screenshot below:
The above is a small series for everyone to bring the Java reflection to get an object by reflection of the method information (instance code) of the whole content, hope to help everyone, a lot of support cloud Habitat Community ~