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 obtain the information of the class, first you need to obtain the class type of the class C = obj.getClass();//What subclass object is passed? c is the class type of the subclass //Get the name of the class System.out.println("The name of the class is:"+c.getName());/* * Method class, method object* A member method is a Method object* getMethods() method obtains all public functions, including inherited from the parent class* getDeclaredMethods() gets all methods declared by the class themselves, and the location access permissions */Method[] methods = c.getMethods();//for(int i=0;i<methods.length;i++){//Get the class type of the return value type of the method Class returnType = methods[i].getReturnType();System.out.print(returnType.getName()+" ");//Get the name of the method System.out.print(methods[i].getName()+"(");//Get the parameter type--》The type of the parameter list declass type Class[] paramType = methods[i].getParameterTypes(); for(Class class1:paramType){System.out.print(class1.getName()+",");}System.out.println(")");}}}//output://The name of the class is: java.lang.String//boolean equals(java.lang.Object,)//java.lang.String toString()//int hashCode()//.......The following code is the 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);}}The screenshot is as follows:
The above is the full content of Java reflection, which I brought to you through reflection, to obtain an object's method information (example code). I hope it will be helpful to everyone and support Wulin.com more~