import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
//反射获取构造方法并实例化对象
public class ReflectDemo2 {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//获取字节码对象
Class<Student> studentClass = Student.class;
//获取构造方法
// Constructor<?>[] constructors = studentClass.getConstructors();
// for (Constructor<?> constructor : constructors) {
// System.out.println(constructor);
// }
// Constructor<?>[] declaredConstructors = studentClass.getDeclaredConstructors();
// for (Constructor<?> declaredConstructor : declaredConstructors) {
// System.out.println(declaredConstructor);
// }
//
// Constructor<Student> constructor = studentClass.getConstructor();
// System.out.println(constructor);
Constructor<Student> declaredConstructor = studentClass.getDeclaredConstructor();
// System.out.println(declaredConstructor);
//无参构造实例化对象
Student student = declaredConstructor.newInstance();
System.out.println(student);
//有参构造实例化对象
Constructor<Student> constructor = studentClass.getConstructor(String.class, int.class, String.class);
Student student1 = constructor.newInstance("郭奉孝", 24, "魏国");
System.out.println(student1);
}
}