什么是反射机制
在程序运行状态中,对于任意一个类或对象,都能够获取到这个类的所有属性和方法(包括私有属性和方法),这种动态获取信息以及动态调用对象方法的功能就称为反射机制。简单来讲,通过反射,类对我们是完全透明的,想要获取任何东西都可以
反射有什么优点
可以在程序运行过程中,操作这些对象;
可以解耦,提高程序的可扩展性
代码演示说明
编写一个类,然后通过反射来读、操作这个类
package com.yenroc.fanshe;
import java.math.BigDecimal;
public class Employee {
public String id;
String name;
protected Integer age;
private BigDecimal salary;
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public Employee(String id, String name, Integer age){
this.id = id;
this.name = name;
this.age = age;
}
// 编写私有构建方法,需要声明public 无参构建,不然不能new Employee()
public Employee(){
}
public void iSleep(){
System.out.println("在睡觉。。");
}
private void iLook(){
System.out.println("在看。。");
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
反射代码示例
package com.yenroc.fanshe;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class FansheTest {
public static void main(String[] args) throws Exception {
getIClass();// 获取class
getIClassInfo(Employee.class);// 获取Class类信息[变量,构造方法,方法]
classField();// field获取值赋值操作
classConstructor();// 构造方法的操作newInstance
classMethod();// 方法对象 invoke
}
/**
* getClass:获取Class对象的三种方式
*/
private static void getIClass() throws Exception {
// 1. 源代码阶段
Class forNameClass = Class.forName("com.yenroc.fanshe.Employee");
System.out.println("forNameClass=" + forNameClass);// forNameClass=class com.yenroc.fanshe.Employee
//2. 类名
Class className = Employee.class;
System.out.println("className=" + className);//className=class com.yenroc.fanshe.Employee
//3. 运行时instance.getClass();
Employee emp = new Employee();
Class instanceClass = emp.getClass();
System.out.println("instanceClass=" + instanceClass);// instanceClass=class com.yenroc.fanshe.Employee
System.out.println(forNameClass == className);//true
System.out.println(className == instanceClass);//true
}
/**
* 获取Class类信息[变量,构造方法,方法]
*/
private static void getIClassInfo(Class clazz) throws Exception {
Field[] publicFields = clazz.getFields();//获取所有public修饰的成员变量
Field[] allFields = clazz.getDeclaredFields();//获取所有的成员变量
for (int i = 0; i < allFields.length; i++) {
//allField=java.lang.String com.yenroc.fanshe.Employee.id
//allField=public java.lang.String com.yenroc.fanshe.Employee.name
//allField=protected java.lang.Integer com.yenroc.fanshe.Employee.age
//allField=private java.math.BigDecimal com.yenroc.fanshe.Employee.salary
System.out.println("allField="+allFields[i]);
}
Constructor<?>[] publicConstrutors = clazz.getConstructors();//获取所有public修饰的构造方法
Constructor<?>[] allConstrutors = clazz.getDeclaredConstructors();//获取所有的构造方法
for (int i = 0; i < allConstrutors.length; i++) {
//allConstrutor=public com.yenroc.fanshe.Employee(java.lang.String,java.lang.String)
//allConstrutor=public com.yenroc.fanshe.Employee()
System.out.println("allConstrutor="+allConstrutors[i]);
}
Method[] publicMethods = clazz.getMethods();// 获取所有public修饰的方法
Method[] allMethods = clazz.getDeclaredMethods();// 获取所有的方法
for (int i = 0; i < allMethods.length; i++) {
//allMethod=public java.lang.String com.yenroc.fanshe.Employee.toString()
//allMethod=public void com.yenroc.fanshe.Employee.iSleep()
//allMethod=public void com.yenroc.fanshe.Employee.setSalary(java.math.BigDecimal)
//allMethod=private void com.yenroc.fanshe.Employee.iLook()
//allMethod=public java.math.BigDecimal com.yenroc.fanshe.Employee.getSalary()
System.out.println("allMethod="+allMethods[i]);
}
}
/**
* field获取值赋值操作
*/
private static void classField() throws Exception {
Class clazz = Employee.class;
Field idField = clazz.getField("id");// 获取指定名称的public修饰的成员变量
/**
* getDeclaredField可以获取到name,age,salary等字段,但是因为不是public修饰,
* 但不能直接set和get,必须忽略访问权限修饰符的安全检查后才可以
* idField.setAccessible(true);忽略访问权限修饰符的安全检查,又称为暴力反射
*/
Employee emp = new Employee();
String idValue = (String)idField.get(emp);// 获取emp对象的id字段的值,因为是new的,所以获取出来的是null
System.out.println("emp的idValue="+ idValue);// emp的idValue=null
idField.set(emp,"aaaaaa");// 给emp的id字段赋值
System.out.println(emp);// Employee [id=aaaaaa, name=null, age=null, salary=null]
}
/**
* 构造方法的操作newInstance
*/
private static void classConstructor() throws Exception {
Class clazz = Employee.class;
Constructor constructor = clazz.getConstructor();//获取无参构造函数
Object empInstance = constructor.newInstance();
System.out.println("empInstance = " + empInstance);// empInstance = Employee [id=null, name=null, age=null, salary=null]
Constructor constructor2 = clazz.getConstructor(String.class,String.class,Integer.class);// id,name,age 的构造方法
Object empInstance2 = constructor2.newInstance("aaa", "yenroc", 25);
System.out.println("empInstance2 = " + empInstance2);//empInstance2 = Employee [id=aaa, name=yenroc, age=25, salary=null]
}
/**
* 方法对象
*/
private static void classMethod() throws Exception {
Class clazz = Employee.class;
Method iSleepMethod = clazz.getMethod("iSleep");//iLook 会找不到方法
Employee emp = new Employee();
Object rtValue = iSleepMethod.invoke(emp);//iSleep() 方法返回值,如果没有rtValue 就是null
System.out.println("rtValue="+rtValue);//在睡觉。。\r rtValue=null
}
}