Java反射
反射:框架設計的靈魂
框架:半成品,可以在框架的基礎上進行軟體開發,簡化編碼
反射:將類的各個組成部分封裝為其他的物件,這就是反射機制
好處:
1.可以在程式的運行程序中,操作這些物件
2.可以解耦,提高程式的可拓展性
下面我們用一張圖來講述java的反射機制

由圖可見將類的各個組成部分封裝為一個個物件,成員變數封裝為field物件,構造方法封裝為con物件,成員方法封裝圍為method物件
獲取Clss物件的三種方式
- Class.forName("全類名"):將位元組碼檔案加載進記憶體,回傳Class物件
* 多用于組態檔,將類名定義在組態檔中,讀取檔案,加載類 - 類名.class:通過類名的屬性class獲取
* 多用于引數的傳遞 - 物件.getClass():getClass()方法在Object類中定義著,
* 多用于物件的獲取位元組碼的方式
代碼
public class Main {
public static void main(String[] args) throws ClassNotFoundException {
Class<?> person = Class.forName("Reflect.Person");
System.out.println(person);
Class<Person> personClass = Person.class;
System.out.println(personClass);
Person person1 = new Person();
Class<? extends Person> aClass = person1.getClass();
System.out.println(aClass);
}
}
結果

可見一個類,在一次運行程序中只會被加載一次
那么class物件有什么用呢?
獲取功能:
1. 獲取成員變數們
* Field[] getFields() :獲取所有public修飾的成員變數
* Field getField(String name) 獲取指定名稱的 public修飾的成員變數
* Field[] getDeclaredFields() 獲取所有的成員變數,不考慮修飾符
* Field getDeclaredField(String name)
package Reflect;
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws ClassNotFoundException {
//獲取class物件
Class<Person> personClass = Person.class;
//使用getFields方法獲取public修飾的方法
Field[] fields = personClass.getFields();
System.out.println("getFields方法:");
//獲取public修飾的成員變數
for (Field field : fields) {
System.out.println(field);
}
System.out.println("getDeclaredFields方法:");
//使用getDeclareFields方法獲取全部成員變數
Field[] declaredFields = personClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
}
}
結果:

2. 獲取構造方法們
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(類<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(類<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
package Reflect;
import java.lang.reflect.Constructor;
public class Main {
public static void main(String[] args) throws ClassNotFoundException {
//獲取class物件
Class<Person> personClass = Person.class;
//獲取public 修飾的構造方法
Constructor<?>[] constructors = personClass.getConstructors();
System.out.println("Public修飾的構造方法:");
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
//獲取所有構造方法
System.out.println("所有構造方法:");
Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
for (Constructor<?> declaredConstructor : declaredConstructors) {
System.out.println(declaredConstructor);
}
}
}
結果:

3. 獲取成員方法們:
* Method[] getMethods()
* Method getMethod(String name, 類<?>... parameterTypes)
* Method[] getDeclaredMethods()
* Method getDeclaredMethod(String name, 類<?>... parameterTypes)
4. 獲取全類名
* String getName()
*
package Reflect;
public class Main {
public static void main(String[] args) throws ClassNotFoundException {
//獲取class物件
Class<Person> personClass = Person.class;
//獲取全類名
String name = personClass.getName();
System.out.println(name);
}
}
結果

Field:成員變數
* 操作:
1. 設定值
* void set(Object obj, Object value)
2. 獲取值
* get(Object obj)
3. 忽略訪問權限修飾符的安全檢查
* setAccessible(true):暴力反射
Constructor:構造方法
創建物件:
* T newInstance(Object... initargs)
* 如果使用空引數構造方法創建物件,操作可以簡化:Class物件的newInstance方法
package Reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//獲取class物件
Class<Person> personClass = Person.class;
Person person = new Person();
Constructor<Person> one = personClass.getConstructor(String.class, int.class);
System.out.println(one);
//構造方法1
System.out.println("構造方法1:");
Person zhangsan = one.newInstance("zhangsan", 11);
System.out.println(zhangsan);
System.out.println("構造方法2:");
Person person2 = personClass.newInstance();
System.out.println(person2);
}
}
結果

Method:方法物件
執行方法:
Object invoke(Object obj, Object... args)
獲取方法名稱:
String getName:獲取方法名
以上就是java反射的一些基礎知識,如有錯誤還請各位批評指正,喜歡我的可以點贊收藏,也可以關注呀

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/137107.html
標籤:Java
