需要的Student实体类
public class Student { private String name = "小黑黑"; public Integer age; protected Date birthday; int num; static { System.out.println("student的静态代码块"); } public static void testStudent() { System.out.println("这是静态方法...."); } // 私有的方法 private int getNum(int num) { return num + 10; } /* * 在获取 私有属性值的时候 需要注释掉 private Student() { * System.out.println("这是Student无参构造"); } */ public Student() { System.out.println("这是Student无参构造"); } public Student(String name, int age, Date birthday) { System.out.println("这是Student带参构造"); this.name = name; this.age = age; this.birthday = birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
测试类代码
public class ReflectTest { // 反射 /** * 所有类的对象 都是Class的实例 * 所有类的父类都是Object */ @Test public void getClassName() { Student stu = new Student(); System.out.println(stu.getClass().getSuperclass()); System.out.println(Student.class.getSuperclass()); System.out.println(String.class.getSuperclass()); System.out.println(Integer.class.getSuperclass().getSuperclass()); } // 三种方式 获取 指定类的 完整限定名 @Test public void getClazz() throws Exception { System.out.println(Class.forName("cn.bdqn.test.Student").getName()); System.out.println(new Student().getClass().getName()); System.out.println(Student.class.getName()); } // 通过反射获取指定类的信息 @Test public void getMsg() throws Exception { Class c = Class.forName("cn.bdqn.test.Student"); System.out.println("全类名:" + c.getName()); System.out.println("包名:" + c.getPackage().getName()); System.out.println("类名:" + c.getSimpleName()); int num = c.getModifiers(); System.out.println(num); System.out.println(Modifier.toString(num)); } // 通过反射获取属性的信息 @Test public void getFileds() throws Exception { Class c = Class.forName("cn.bdqn.test.Student"); // 获取所有public修饰的字段 Field[] fields = c.getFields(); for (int i = 0; i < fields.length; i++) { System.out.println(fields[i]); } System.out.println("获取所有的属性名称****************"); fields = c.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { System.out.println(fields[i].getName()); } System.out.println("获取所有属性的修饰符*****************"); // default的int值 是 0 for (int i = 0; i < fields.length; i++) { System.out.println(Modifier.toString(fields[i].getModifiers())); } } // 通过反射观察执行顺序 @Test public void getC() throws Exception { Class c = Class.forName("cn.bdqn.test.Student"); // 加载静态代码块 // Student.testStudent(); // Student student = new Student(); // 耦合 无参构造 Student student = (Student) c.newInstance(); // 解耦合 无参构造 } // 通过反射获取类中的构造方法 @Test public void getConstructors() throws Exception { Class c = Class.forName("cn.bdqn.test.Student"); // 加载静态代码块 Constructor[] constructors = c.getDeclaredConstructors(); for (int i = 0; i < constructors.length; i++) { System.out.println("构造的修饰符:" + Modifier.toString(constructors[i].getModifiers())); System.out.println("构造的名称:" + constructors[i].getName()); // 1.8才有getParameterCount System.out .println("构造的参数个数:" + constructors[i].getParameterCount()); Class[] types = constructors[i].getParameterTypes(); // 参数的类型 for (Class class1 : types) { System.out.println("参数的类型:" + class1.getName()); System.out.println("参数的类型简写:" + class1.getSimpleName()); } System.out.println("******************************"); } } // 通过反射获取类中的构造方法 @Test public void setFiledName() throws Exception { Student student = Student.class.newInstance(); Class c = student.getClass(); // 获取指定的属性 Field field = c.getDeclaredField("name"); // 设置属性的访问权限 field.setAccessible(true); System.out.println(field.get(student)); } // 通过反射获取类中的构造方法 @SuppressWarnings("unchecked") @Test public void getMethod() throws Exception { Student student = Student.class.newInstance(); Class c = student.getClass(); System.out.println(student.age); Method method = c.getDeclaredMethod("setAge", Integer.class); // 执行方法 method.invoke(student, 50); System.out.println(student.age); // 执行私有的带有参数的 并且有返回值的方法 method = c.getDeclaredMethod("getNum", int.class); // 开启方法的权限 method.setAccessible(true); int num = (Integer) method.invoke(student, 50); System.out.println(num); } }