1.什么是反射?
反射就是类在运行期间,把类中成员抽取为其他类的过程就是反射。
反射是设计框架的灵魂。
学过的框架:
1.mybatis框架--持久化框架;
2.springmvc框架--mvc框架;
3.spring框架
2.为什么使用反射?
反射是为了解决在运行期间,对某个实例一无所知的情况下,如何调用其方法或属性。
3.获取Class反射类的方式
有三种模式:
1.通过类名.class属性
Class<Student> aClass=Student.class;
2.通过类路径获取
Class aClass1=Class.forName("com.lr.Student");
3.通过对象名获取反射类型
Student student=new Student(); Class aClass2 = student.getClass();
4.Class类中常用的方法
1.根据反射类得到实例对象--newInstance()
2.获取反射类上的注解对象--getAnnotation()
Class<Student> studentClass = Student.class; Student student = studentClass.newInstance(); System.out.println(student); // 通过反射类上的注解--反射是在运行时得到 Annotation annotation = studentClass.getAnnotation(Annotation.class); System.out.println(annotation); // 得到注解上的值 System.out.println(annotation.value());
Student{name='null', age=null} @com.lr.Annotation(value=lr) lr
5.获取Method方法类对象
getDeclaredMethods():获得本类中所有的方法。
getDeclaredMethod("方法名",参数类型):获取本类中指定的方法。
getMethods():获取本类以及父辈类中public修饰的方法。
getMethod("方法名",参数类型):获取本类以及父辈类指定的方法。
public static void main(String[] args) throws Exception { Class<Student> studentClass = Student.class; // 得到本类中所有方法 Method[] declaredMethods = studentClass.getDeclaredMethods(); // 增强for循环--遍历出所有的方法 for(Method m:declaredMethods){ System.out.println(m); } System.out.println("~~~~~~~~~~~~~~~~~~~~``~~~~~~~~~~~~~~~~~~~~``~~~~~~~~~~~~~~~~~~~~``"); // 获取本类中指定的方法 Method show = studentClass.getDeclaredMethod("show"); System.out.println(show); // 获取本类中指定的带参数的方法 Method fun = studentClass.getDeclaredMethod("fun",int.class); System.out.println(fun); System.out.println("~~~~~~~~~~~~~~~~~~~~``~~~~~~~~~~~~~~~~~~~~``~~~~~~~~~~~~~~~~~~~~``"); // 获取本类以及父辈类所有public的方法 Method[] methods = studentClass.getMethods(); for(Method m:methods){ System.out.println(m); } // 获取本类以及父辈类指定的public的方法--就近原则 Method print = studentClass.getMethod("print"); System.out.println(print); }
6.Methods类对象中常用的方法
执行该方法:Object o=invoke(Object对象,参数值)
设置允许访问私有成员:私有属性或方法.setAccessible()
public static void main(String[] args) throws Exception { // ?表示泛型通配符 Class<?> aClass = Class.forName("com.lr.People"); // 通过反射类得到反射类的类对象 Object o = aClass.newInstance(); // 获取本类以及父辈类指定的方法--无参数类型的 Method print = aClass.getMethod("print"); // 执行该方法--返回该方法执行的结果 // 第一个参数表示执行方法的对象 Object invoke = print.invoke(o); System.out.println(invoke); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); // 获取本类以及父辈类指定的方法--有参数类型的 Method myAge = aClass.getMethod("myAge", int.class); Object invoke1 = myAge.invoke(o, 23); System.out.println(invoke1); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); // 获取本类中指定的方法 Method print02 = aClass.getDeclaredMethod("print02"); // 使用强力反射就可以访问私有的方法 print02.setAccessible(true);//设置允许访问私有成员 Object invoke2 = print02.invoke(o); System.out.println(invoke2); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); // 得到方法上指定的注解 Annotation annotation = myAge.getAnnotation(Annotation.class); System.out.println(annotation); // 方法名 String name = myAge.getName(); System.out.println(name); }
7.获取Field属性对象的方式
8.Field类中常用的方法
set(Object对象,值):为属性赋值
getName():获取属性名
getAnnotation():获取属性上的注解对象
Class<Student> studentClass = Student.class; //获取所有属性名 Field[] declaredFields = studentClass.getDeclaredFields(); for (Field f:declaredFields){ System.out.println(f.getName()); // 获取每个属性对象上的注解对象 Annotation annotation = f.getAnnotation(Annotation.class); String value = annotation.value(); System.out.println(value); }
9.案例
Teacher类
@Data public class Teacher { String name; String age; }
Test01类
public class Test01 { public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { // 1.加载属性文件 InputStream resourceAsStream = Test01.class.getClassLoader().getResourceAsStream("qy174.properties"); // 2.通过属性类Properties Properties properties=new Properties(); properties.load(resourceAsStream); // 3.属性类读取属性文件中指定的key的值 String className = properties.getProperty("className"); // 4.根据类路劲得到反射对象 Class<?> aClass = Class.forName(className); // 5.根据反射类创建类对象 Object o = aClass.newInstance(); System.out.println(o); // 6.为属性赋值 Field[] declaredFields = aClass.getDeclaredFields(); for (Field f:declaredFields){ f.setAccessible(true); f.set(o,new Random().nextInt(100)+"1"); } System.out.println(o); } }
resource
className=com.test02.Teacher