什麼是反射機制
在程序運行狀態中,對於任意一個類或對象,都能夠獲取到這個類的所有屬性和方法(包括私有屬性和方法),這種動態獲取信息以及動態調用對象方法的功能就稱爲反射機制。簡單來講,通過反射,類對我們是完全透明的,想要獲取任何東西都可以
反射有什麼優點
可以在程序運行過程中,操作這些對象;
可以解耦,提高程序的可擴展性
代碼演示說明
編寫一個類,然後通過反射來讀、操作這個類
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
}
}