ConstructorExercise
package ConstructorExercise20240831;
import ClassReflect20240831.Student;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class CreateInstance {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
// 通过反射获取其构造方法,并实例化对象
// Instance:实例
// 方法:newInstance(),需要用Constructor对象调用
// 获得Student的Class对象
Class<?> studentClass = Class.forName("ClassReflect20240831.Student");
// 获得Student的构造方法Constructor对象
Constructor<?> constructor = studentClass.getConstructor(String.class, String.class, int.class);
// 通过Constructor对象的newInstance方法创建对象
Student student = (Student) constructor.newInstance("张三", "男", 18);
// newInstance中的参数就是Constructor对应的构造方法的形参
System.out.println(student);
System.out.println("----------------------------------");
// 即使通过getDeclaredConstructor方法获得了private修饰的构造方法,也不能直接使用,否则报错
Constructor<?> constructor2 = studentClass.getDeclaredConstructor(String.class);
// 要使用setAccessible()方法临时取消其权限,才可以正常使用,这个操作也称为“暴力反射”
constructor2.setAccessible(true);
Student student2 = (Student) constructor2.newInstance("李四");
System.out.println(student2);
}
}
Student
package ClassReflect20240831;
public class Student {
private String name;
private String gender;
public int age;
public Student() {}
private Student(String name){
this.name = name;
}
private Student(int age){
this.age = age;
}
private Student(String name, String gender){
this.name = name;
this.gender = gender;
}
public Student(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
public void eat(String food) {
System.out.println("学生" + this.name + "正在吃" + food);
}
private void study() {
System.out.println("学生" + this.name + "正在学习");
}
}
RefletExercise2
package ConstructorExercise20240831;
import java.lang.reflect.Constructor;
public class ReflectExercise2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
// 反射获取类中的构造方法 Constructor
// 反射都是从class字节码文件中获取的内容,所以说需要先获得Class对象,再对其进行操作
Class<?> studentClass = Class.forName("ClassReflect20240831.Student");
// Constructor<?>[] getConstructor() 返回一个Constructor对象数组,包含所有以public修饰的构造方法
// Constructor<?>[] getDeclaredConstructor() 返回一个Constructor对象数组,包含所有的构造方法
Constructor<?>[] constructors1 = studentClass.getConstructors();
for (Constructor<?> con : constructors1) {
// 成功得到了所有被public修饰的构造方法并遍历
System.out.println(con);
}
System.out.println("---------------------------------");
Constructor<?>[] constructors2 = studentClass.getDeclaredConstructors();
for (Constructor<?> con : constructors2) {
// 成功得到了Student类中所有的构造方法,包含所有权限修饰的构造方法
System.out.println(con);
}
System.out.println("---------------------------------");
// Constructor<T> getConstructor(Class<?>...parameterTypes) 获得由public修饰的指定构造(传递构造方法形参的参数类型对象)
// Constructor<T> getConstructor(Class<?>...parameterTypes) 获得指定构造(传递构造方法形参的参数类型对象),无视权限
Constructor<?> constructor3 = studentClass.getConstructor(String.class, String.class, int.class);
System.out.println(constructor3);
// 如果用getConstructor方法获取非public修饰的构造方法,则会报错:NoSuchMethodException
/* Constructor<?> constructor4 = studentClass.getConstructor(String.class);
System.out.println(constructor4); */
// 如果用getDeclaredConstructor方法,则可以无视权限修饰,获取指定的构造方法
Constructor<?> constructor4 = studentClass.getDeclaredConstructor(String.class);
System.out.println(constructor4);
}
}
标签:反射,Java,String,构造方法,Student,Constructor,public,name From: https://blog.csdn.net/Aishangyuwen/article/details/141760909