转:Java反射教程
原文來自于:http://www.importnew.com/9078.html
什么是反射?反射有什么用處?
1. 什么是反射?
“反射(Reflection)能夠讓運行于JVM中的程序檢測和修改運行時的行為。”這個概念常常會和內省(Introspection)混淆,以下是這兩個術語在Wikipedia中的解釋:
從它們的定義可以看出,內省是反射的一個子集。有些語言支持內省,但并不支持反射,如C++。
內省示例:instanceof?運算符用于檢測某個對象是否屬于特定的類。
| 1 2 3 4 | if (obj instanceof Dog) { ????Dog d = (Dog) obj; ????d.bark(); } |
反射示例:Class.forName()方法可以通過類或接口的名稱(一個字符串或完全限定名)來獲取對應的Class對象。forName方法會觸發類的初始化。
| 1 2 3 4 5 | // 使用反射 Class<?> c = Class.forName("classpath.and.classname"); Object dog = c.newInstance(); Method m = c.getDeclaredMethod("bark", new Class<?>[0]); m.invoke(dog); |
在Java中,反射更接近于內省,因為你無法改變一個對象的結構。雖然一些API可以用來修改方法和屬性的可見性,但并不能修改結構。
2. 我們為何需要反射?
反射能夠讓我們:
- 在運行時檢測對象的類型;
- 動態構造某個類的對象;
- 檢測類的屬性和方法;
- 任意調用對象的方法;
- 修改構造函數、方法、屬性的可見性;
- 以及其他。
反射是框架中常用的方法。
例如,JUnit通過反射來遍歷包含?@Test?注解的方法,并在運行單元測試時調用它們。(這個連接中包含了一些JUnit的使用案例)
對于Web框架,開發人員在配置文件中定義他們對各種接口和類的實現。通過反射機制,框架能夠快速地動態初始化所需要的類。
例如,Spring框架使用如下的配置文件:
| 1 2 3 | <bean id="someID" class="com.programcreek.Foo"> ????<property name="someField" value="someValue" /> </bean> |
當Spring容器處理<bean>元素時,會使用Class.forName("com.programcreek.Foo")來初始化這個類,并再次使用反射獲取<property>元素對應的setter方法,為對象的屬性賦值。
Servlet也會使用相同的機制:
| 1 2 3 4 | <servlet> ????<servlet-name>someServlet</servlet-name> ????<servlet-class>com.programcreek.WhyReflectionServlet</servlet-class> <servlet> |
3. 如何使用反射?
讓我們通過幾個典型的案例來學習如何使用反射。
示例1:獲取對象的類型名稱。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package myreflection; import java.lang.reflect.Method; public class ReflectionHelloWorld { ????public static void main(String[] args){ ????????Foo f = new Foo(); ????????System.out.println(f.getClass().getName());???????? ????} } class Foo { ????public void print() { ????????System.out.println("abc"); ????} } |
輸出:
| 1 | myreflection.Foo |
示例2:調用未知對象的方法。
在下列代碼中,設想對象的類型是未知的。通過反射,我們可以判斷它是否包含print方法,并調用它。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package myreflection; import java.lang.reflect.Method; public class ReflectionHelloWorld { ????public static void main(String[] args){ ????????Foo f = new Foo(); ????????Method method; ????????try { ????????????method = f.getClass().getMethod("print", new Class<?>[0]); ????????????method.invoke(f); ????????} catch (Exception e) { ????????????e.printStackTrace(); ????????}?????????? ????} } class Foo { ????public void print() { ????????System.out.println("abc"); ????} } |
輸出:
| 1 | abc |
示例3:創建對象
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package myreflection; public class ReflectionHelloWorld { ????public static void main(String[] args){ ????????// 創建Class實例 ????????Class<?> c = null; ????????try{ ????????????c=Class.forName("myreflection.Foo"); ????????}catch(Exception e){ ????????????e.printStackTrace(); ????????} ????????// 創建Foo實例 ????????Foo f = null; ????????try { ????????????f = (Foo) c.newInstance(); ????????} catch (Exception e) { ????????????e.printStackTrace(); ????????}?? ????????f.print(); ????} } class Foo { ????public void print() { ????????System.out.println("abc"); ????} } |
示例4:獲取構造函數,并創建對象。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package myreflection; import java.lang.reflect.Constructor; public class ReflectionHelloWorld { ????public static void main(String[] args){ ????????// 創建Class實例 ????????Class<?> c = null; ????????try{ ????????????c=Class.forName("myreflection.Foo"); ????????}catch(Exception e){ ????????????e.printStackTrace(); ????????} ????????// 創建Foo實例 ????????Foo f1 = null; ????????Foo f2 = null; ????????// 獲取所有的構造函數 ????????Constructor<?> cons[] = c.getConstructors(); ????????try { ????????????f1 = (Foo) cons[0].newInstance(); ????????????f2 = (Foo) cons[1].newInstance("abc"); ????????} catch (Exception e) { ????????????e.printStackTrace(); ????????}?? ????????f1.print(); ????????f2.print(); ????} } class Foo { ????String s; ????public Foo(){} ????public Foo(String s){ ????????this.s=s; ????} ????public void print() { ????????System.out.println(s); ????} } |
輸出:
| 1 2 | null abc |
此外,你可以通過Class實例來獲取該類實現的接口、父類、聲明的屬性等。
示例5:通過反射來修改數組的大小。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package myreflection; import java.lang.reflect.Array; public class ReflectionHelloWorld { ????public static void main(String[] args) { ????????int[] intArray = { 1, 2, 3, 4, 5 }; ????????int[] newIntArray = (int[]) changeArraySize(intArray, 10); ????????print(newIntArray); ????????String[] atr = { "a", "b", "c", "d", "e" }; ????????String[] str1 = (String[]) changeArraySize(atr, 10); ????????print(str1); ????} ????// 修改數組的大小 ????public static Object changeArraySize(Object obj, int len) { ????????Class<?> arr = obj.getClass().getComponentType(); ????????Object newArray = Array.newInstance(arr, len); ????????// 復制數組 ????????int co = Array.getLength(obj); ????????System.arraycopy(obj, 0, newArray, 0, co); ????????return newArray; ????} ????// 打印 ????public static void print(Object obj) { ????????Class<?> c = obj.getClass(); ????????if (!c.isArray()) { ????????????return; ????????} ????????System.out.println("\nArray length: " + Array.getLength(obj)); ????????for (int i = 0; i < Array.getLength(obj); i++) { ????????????System.out.print(Array.get(obj, i) + " "); ????????} ????} } |
輸出:
| 1 2 3 4 | Array length: 10 1 2 3 4 5 0 0 0 0 0 Array length: 10 a b c d e null null null null null |
總結
上述示例代碼僅僅展現了Java反射機制很小一部分的功能。如果你覺得意猶未盡,可以前去閱讀官方文檔。
參考資料:
原文鏈接:?Programcreek?翻譯:?ImportNew.com?-?薄荷腦
譯文鏈接:?http://www.importnew.com/9078.html
轉載于:https://www.cnblogs.com/guoyongrong/p/3535509.html
總結
以上是生活随笔為你收集整理的转:Java反射教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html5爱心表白
- 下一篇: Use Selenium webdriv