package com.imekaku.reflect; import java.lang.reflect.Method; /** * Created by lee on 17/2/23. */ public class ClassUtils { public static void printClassMessage(Object obj) { Class c = obj.getClass(); System.out.println("类名称是:"+c.getName()); // c.getMethods(); // public 方法,包括从父类继承而来的 // c.getDeclaredMethods(); // 自己所有的方法,不问访问权限 Method[] ms = c.getMethods(); for (Method m : ms) { Class returnType = m.getReturnType(); // 得到返回值的类类型 System.out.print(returnType.getName()+" "); System.out.print(m.getName()+"("); Class[] paramTypes = m.getParameterTypes(); for (Class class1 : paramTypes) { System.out.print(class1.getName()+","); } System.out.println(")"); } } }
package com.imekaku.reflect; /** * Created by lee on 17/2/23. */ public class ClassDemo03 { public static void main(String[] args) { String s = "hi"; ClassUtils.printClassMessage(s); } }