public class Test { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException { Class c1 = Class.forName(“lesson02.Test”); System.out.println(c1.getName());//获得包名+类名
System.out.println(c1.getSimpleName());//获得类名 //获得类的属性 System.out.println("--------------------------"); Field[] fields = c1.getFields(); //只能找到public属性 fields = c1.getDeclaredFields(); for (Field field:fields//找到全部的属性 ) { System.out.println(field); } Field name = c1.getDeclaredField("name");//指定属性 System.out.println(name); System.out.println("========================"); Method[] methods = c1.getMethods();//获得本类及其父类的所有方法 for (Method method : methods) { System.out.println("正常的"+method); } methods=c1.getDeclaredMethods(); for (Method method : methods) { System.out.println("getDeclaredMethods"+method); } //获得指定方法 //重载 Method getName = c1.getMethod("getName",null); Method setName =c1.getMethod("setName",String.class); // }
}