1、获取Class类对象
java">
Class<?> aClass1 = Class.forName("com.itheima.d2_reflect.Student");
Class<Student> aClass2 = Student.class;
Class<? extends Student> aClass3 = new Student().getClass();
2、获取Student类的构造器对象
java">Class<Student> aClass = Student.class;
Constructor<?> constructor1 = aClass.getConstructor();
Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class,int.class);
Constructor<?>[] constructors = aClass.getConstructors();
Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
declaredConstructor.setAccessible(true);
Student student = (Student)declaredConstructor.newInstance("小佳",19);
3、获取Student类中的各个属性Field对象
java">Class<Student> aClass = Student.class;
Field[] fields = aClass.getFields();
Field[] declaredFields = aClass.getDeclaredFields();
Field gender1 = aClass.getField("gender");
Field name2 = aClass.getDeclaredField("name");
Student student = aClass.newInstance();
name2.setAccessible(true);
name2.set(student,"小蒋");
4、获取Student类中的各个方法Method对象
java">Class<Student> aClass = Student.class;
Method[] methods = aClass.getMethods();
Method[] declaredMethods = aClass.getDeclaredMethods();
Method getGender = aClass.getMethod("getGender");
Method show = aClass.getDeclaredMethod("show");
Method setGender = aClass.getDeclaredMethod("setGender", String.class);
Student student1 = aClass.newInstance();
String str_toString = (String)toString.invoke(student1);
System.out.println("str_toString = " + str_toString);
setGender.invoke(student1,"男");
System.out.println("student1 = " + student1);
show.setAccessible(true);
show.invoke(student1);