浅谈Java反射1https://blog.csdn.net/qq_48817343/article/details/137826708?spm=1001.2014.3001.5502
保存任意数据--练习
package com.reflect5; /** * @author yl * @createDate 2023/4/12 * @description */ public class Student { private String name; private Integer age; private String gender; private Double height; private String hobby; public Student() { } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", height=" + height + ", hobby='" + hobby + '\'' + '}'; } public Student(String name, Integer age, String gender, Double height, String hobby) { this.name = name; this.age = age; this.gender = gender; this.height = height; this.hobby = hobby; } 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 String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Double getHeight() { return height; } public void setHeight(Double height) { this.height = height; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } }
package com.reflect5; /** * @author yl * @createDate 2023/4/15 * @description */ public class Teacher { private String name; private Integer salary; public Teacher() { } @Override public String toString() { return "Teacher{" + "name='" + name + '\'' + ", salary=" + salary + '}'; } public Teacher(String name, Integer salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getSalary() { return salary; } public void setSalary(Integer salary) { this.salary = salary; } }
package com.reflect5; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; /** * @author yl * @createDate 2023/4/14 * @description */ public class MyReflectDemo { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, IOException { /* 对任意一个对象都可以把对象的所有字段和值,保存到文件中去 */ Student student = new Student("小红", 18, "0", 170.0, "随机"); Teacher teacher = new Teacher("波妞", 10000); saveObject(teacher); } // 保存对象的所有成员变量和值保存到本地文件中 public static void saveObject(Object obj) throws IllegalAccessException, IOException { //1.获取字节码文件对象 Class classz = obj.getClass(); //2.创建IO流 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("E:\\study\\java\\proxytest\\src\\com\\a.text")); //3.获取所有的成员变量 Field[] declaredFields = classz.getDeclaredFields(); for (Field declaredField : declaredFields) { declaredField.setAccessible(true); // 获取成员变量的名字 String name = declaredField.getName(); //获取成员变量的值 Object value = declaredField.get(obj); //写出数据 bufferedWriter.write(name + "=" + value); bufferedWriter.newLine(); } bufferedWriter.close(); } }
跟配置文件结合动态创建
反射可以跟配置文件结合的方式,动态的常创建对象,并调用方法