欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

黑马JAVAP190反射的作用:绕过编译阶段,做企业级通用框架

时间:2023-07-05

 

 

 

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 list1 = new ArrayList<>(); ArrayList list2 = new ArrayList<>(); System.out.println(list1.getClass()); System.out.println(list2.getClass()); System.out.println(list1.getClass() == list2.getClass()); //ArrayList.class System.out.println("------------"); ArrayList lists3 = new ArrayList<>(); lists3.add(23); lists3.add(22); //list3.add("黑马") Class c = lists3.getClass(); //ArrayList.class ===> public boolean add(E e) //定位c类中的add方法 Method add = c.getDeclaredMethod("add",Object.class); boolean rs = (boolean) add.invoke(lists3,"黑马"); System.out.println(rs); System.out.println(lists3); ArrayList list4 = lists3; list4.add("白马"); list4.add(false); System.out.println(lists3); }}

 


 

 

 

 

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); }}

 

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。