package com.itheima.d6_reflect_genericity;import java.lang.reflect.Method;import java.util.ArrayList;public class ReflectDemo { public static void main(String[] args) throws Exception { //需求:反射实现泛型擦除后,加入其他类型的元素。 ArrayList
package com.itheima.d7_reflect_framework;import java.io.FileOutputStream;import java.io.PrintStream;import java.lang.reflect.Field;public class MybatisUtil { public static void save(Object obj){ try ( PrintStream ps = new PrintStream(new FileOutputStream("D:\code\javasepromax\junit-reflect-annotation-proxy-app\src\data.txt",true)); ){ //1.提取这个对象的全部 成员变量:只有反射可以解决 Class c = obj.getClass();//c.getSimpleName() 获取当前类名 c.getName获取全限名:包名+类名 ps.println("=============" + c.getSimpleName() + "================"); //2.提取它的全部成员变量 Field[] fields = c.getDeclaredFields(); //3.获取成员变量的信息 for (Field field : fields) { String name = field.getName(); //提取本成员变量在obj对象中的值(取值) field.setAccessible(true); String value = field.get(obj) + ""; ps.println(name + "=" + value); } } catch (Exception e) { e.printStackTrace(); } }}
package com.itheima.d7_reflect_framework;public class Student { private String name; private char sex; private int age; private String className; private String hobby; public Student() { } public Student(String name, char sex, int age, String className, String hobby) { this.name = name; this.sex = sex; this.age = age; this.className = className; this.hobby = hobby; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; }}
package com.itheima.d7_reflect_framework;public class Teacher { private String name; private char sex; private double salary; public Teacher() { } public Teacher(String name, char sex, double salary) { this.name = name; this.sex = sex; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; }}
package com.itheima.d7_reflect_framework;public class ReflectDemo { public static void main(String[] args) throws Exception { Student s = new Student(); s.setName("猪八戒"); s.setClassName("西天跑路1班"); s.setAge(1000); s.setHobby("吃,睡"); s.setSex('男'); MybatisUtil.save(s); Teacher t = new Teacher(); t.setName("波仔"); t.setSex('男'); t.setSalary(6000); MybatisUtil.save(t); }}