image.png

    1. package com.itheima.d7_reflect_framework;
    2. /**
    3. 目标:提供一个通用框架,支持保存所有对象的具体信息。
    4. */
    5. public class ReflectDemo {
    6. public static void main(String[] args) throws Exception {
    7. Student s = new Student();
    8. s.setName("猪八戒");
    9. s.setClassName("西天跑路1班");
    10. s.setAge(1000);
    11. s.setHobby("吃,睡");
    12. s.setSex('男');
    13. MybatisUtil.save(s);
    14. Teacher t = new Teacher();
    15. t.setName("波仔");
    16. t.setSex('男');
    17. t.setSalary(6000);
    18. MybatisUtil.save(t);
    19. }
    20. }
    1. package com.itheima.d7_reflect_framework;
    2. import java.io.FileOutputStream;
    3. import java.io.PrintStream;
    4. import java.lang.reflect.Field;
    5. public class MybatisUtil {
    6. /**
    7. 保存任意类型的对象
    8. * @param obj
    9. */
    10. public static void save(Object obj){
    11. try (
    12. PrintStream ps = new PrintStream(new FileOutputStream("junit-reflect-annotation-proxy-app/src/data.txt", true));
    13. ){
    14. // 1、提取这个对象的全部成员变量:只有反射可以解决
    15. Class c = obj.getClass(); // c.getSimpleName()获取当前类名 c.getName获取全限名:包名+类名
    16. ps.println("================" + c.getSimpleName() + "================");
    17. // 2、提取它的全部成员变量
    18. Field[] fields = c.getDeclaredFields();
    19. // 3、获取成员变量的信息
    20. for (Field field : fields) {
    21. String name = field.getName();
    22. // 提取本成员变量在obj对象中的值(取值)
    23. field.setAccessible(true);
    24. String value = field.get(obj) + "";
    25. ps.println(name + "=" + value);
    26. }
    27. } catch (Exception e) {
    28. e.printStackTrace();
    29. }
    30. }
    31. }
    1. package com.itheima.d7_reflect_framework;
    2. public class Student {
    3. private String name;
    4. private char sex;
    5. private int age;
    6. private String className;
    7. private String hobby;
    8. public Student(){
    9. }
    10. public Student(String name, char sex, int age, String className, String hobby) {
    11. this.name = name;
    12. this.sex = sex;
    13. this.age = age;
    14. this.className = className;
    15. this.hobby = hobby;
    16. }
    17. public String getName() {
    18. return name;
    19. }
    20. public void setName(String name) {
    21. this.name = name;
    22. }
    23. public char getSex() {
    24. return sex;
    25. }
    26. public void setSex(char sex) {
    27. this.sex = sex;
    28. }
    29. public int getAge() {
    30. return age;
    31. }
    32. public void setAge(int age) {
    33. this.age = age;
    34. }
    35. public String getClassName() {
    36. return className;
    37. }
    38. public void setClassName(String className) {
    39. this.className = className;
    40. }
    41. public String getHobby() {
    42. return hobby;
    43. }
    44. public void setHobby(String hobby) {
    45. this.hobby = hobby;
    46. }
    47. }
    1. package com.itheima.d7_reflect_framework;
    2. public class Teacher {
    3. private String name;
    4. private char sex;
    5. private double salary;
    6. public Teacher(){
    7. }
    8. public Teacher(String name, char sex, double salary) {
    9. this.name = name;
    10. this.sex = sex;
    11. this.salary = salary;
    12. }
    13. public String getName() {
    14. return name;
    15. }
    16. public void setName(String name) {
    17. this.name = name;
    18. }
    19. public char getSex() {
    20. return sex;
    21. }
    22. public void setSex(char sex) {
    23. this.sex = sex;
    24. }
    25. public double getSalary() {
    26. return salary;
    27. }
    28. public void setSalary(double salary) {
    29. this.salary = salary;
    30. }
    31. }