Java反射機(jī)制從API到實(shí)例結(jié)合多方資料總結(jié)_第1頁
Java反射機(jī)制從API到實(shí)例結(jié)合多方資料總結(jié)_第2頁
Java反射機(jī)制從API到實(shí)例結(jié)合多方資料總結(jié)_第3頁
Java反射機(jī)制從API到實(shí)例結(jié)合多方資料總結(jié)_第4頁
Java反射機(jī)制從API到實(shí)例結(jié)合多方資料總結(jié)_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、運(yùn)用反射獲取一個(gè)類的方法信息:1. 獲取類的方法(4種etc./ 以下是獲取類的方法/ 1.getClass(方法/ String str = "abc"/ Class class = str.getClass(;System.out.println("第一種獲取類的方法:"Integer i = new Integer(3;Class iClass = i.getClass(; 符合規(guī)格的System.out.println("Simple Name: " + iClass.getSimpleName(;String str = &

2、quot;abc"System.out.println(str.getClass(.getSimpleName(;System.out.println("第二種獲取類的方法: "Class c21 = Class.forName(;Class c22 = Class.forName("ReflectionDemos.MyClass"Class c23 = Class.forName("OperateFile.CopyFile1"System.out.println(c21.getSimpleName(;System.out.

3、println(c22.getSimpleName(;System.out.println(c23.getSimpleName(;/ 3.Class c = String.class;System.out.println("第三種獲取類的方法:"Class iClass2 = int.class;System.out.println(iClass2.getSimpleName(;/ 4.運(yùn)用原始包裝類中的TYPE方法/ Class c1 = Integer.TYPE;/ Class c2 = Boolean.TYPE;/ Class c3 = Void.TYPE;/ Cla

4、ss c4 = Character.TYPE;System.out.println("第四種獲取類的方法:"Class iClass3 = Integer.TYPE;System.out.println(iClass3.getCanonicalName(;System.out.println(iClass3.getSimpleName(;Output:第一種獲取類的方法:Simple Name: IntegerString第二種獲取類的方法: StringMyClassCopyFile1第三種獲取類的方法:int第四種獲取類的方法:intint2. /顯示一個(gè)對象的類名/*

5、 method1 顯示對象的類名*/public static void printClassName(Object obj System.out.println("The class of " + obj.getClass(.getName(;etc./* test method1*/ClassTest ct = new ClassTest(;printClassName(ct;Output:The class of ReflectionDemos.ClassTest3. 獲得類的所有屬性:(包括publicprivateprotected)etc./* method2

6、Class Field getDeclaredFields( 返回 Field* 對象的一個(gè)數(shù)組,這些對象反映此 Class 對象所表示的類或接口所聲明的所有字段。 */Field fields = Class.forName("ReflectionDemos.MyClass".getDeclaredFields(;for (int i = 0; i < fields.length; i+ System.out.println("屬性名:" + fieldsi.getName( + "t屬性類型"+ fieldsi.getTyp

7、e(;Output:屬性名:i 屬性類型int屬性名:f 屬性類型float屬性名:s 屬性類型屬性名:array 屬性類型4. /獲得類的所有方法Method methods = classType.getDeclaredMethods(;methods0.toString(; /此方法的描述etc./* method4 Class Method getDeclaredMethods( 返回 Method* 對象的一個(gè)數(shù)組,這些對象反映此 Class* 對象表示的類或接口聲明的所有方法,包括公共、保護(hù)、默認(rèn)(包)訪問和私有方法,但不包括繼承的方法。*/Method methods = Cla

8、ss.forName("ReflectionDemos.MyClass".getDeclaredMethods(;for (int i = 0; i < methods.length; i+ System.out.println("MyClass類的方法:" + methodsi.getName(;Output:MyClass類的方法:getIMyClass類的方法:setIMyClass類的方法:getFMyClass類的方法:setFMyClass類的方法:getSMyClass類的方法:setS5. Method類的方法:getModifie

9、rs( 返回此Method對象所表示方法的Java語言修飾符getName( 以String形式返回此Method對象表示的方法名稱Annotation getParameterAnnotations( 返回表示按照聲明順序?qū)Υ?Method 對象所表示方法的形參進(jìn)行注釋的那個(gè)數(shù)組的數(shù)組。在Web Service中也有一種運(yùn)用Java標(biāo)注的情況。Class getParameterTypes(:按照聲明順序返回 Class 對象的數(shù)組,這些對象描述了此 Method 對象所表示的方法的形參類型。Etc:/* method6 Class getParameterTypes(:按照聲明順序返回 C

10、lass 對象的數(shù)組,這些對象描述了此* Method 對象所表示的方法的形參類型。*/"下面測試獲取方法形參類型: ""方法名為:" + method.getName(; /private static void setI(int i, String strClass paramClass = method.getParameterTypes(;for (int i = 0; i < paramClass.length; i+ Output:下面測試獲取方法形參類型: 方法名為:setIintStringClass getReturnType(

11、返回一個(gè) Class 對象,該對象描述了此 Method 對象所表示的方法的正式返回類型。 Etc:/* method7 Class getReturnType( 返回一個(gè) Class 對象,該對象描述了此 Method* 對象所表示的方法的正式返回類型。*/System.out.println("下面測試方法的返回類型:"Class returnType = method.getReturnType(;System.out.println(returnType.getSimpleName(;Output: 下面測試方法的返回類型:voidObject invoke(Obj

12、ect obj, Object. args 對帶有指定參數(shù)的指定對象調(diào)用由此 Method 對象表示的底層方法。 6. 方法調(diào)用(invoke)及獲取指定名稱和參數(shù)的方法:A 針對只具有默認(rèn)構(gòu)造函數(shù)的類:/* 運(yùn)用反射機(jī)制調(diào)一個(gè)類的方法*/public class InvokeTester int i;String s;/ public InvokeTester(int i, String s/ this.i = i;/ this.s = s;/ public int add(int param1, int param2 return param1 + param2;public String

13、 echo(String msg return "echo: " + msg;public static void main(String args throws Exception Class classType = InvokeTester.class;/ 獲得該類的一個(gè)默認(rèn)對象(實(shí)例),與方法Object objectCopy=classType.getConstructor(new/ Class.newInstance(new Object;/ 等效Object invokeTester = classType.newInstance(; /注意是默認(rèn)對象,即Inv

14、okeTester類只有默認(rèn)構(gòu)造函數(shù)/ Object invokeTester = classType.getConstructor(new Class int.class, String.class ;/ 調(diào)用InvokeTester對象的add(方法Method addMethod = classType.getMethod("add", new Class int.class,int.class ; /如果第二個(gè)參數(shù)為 new Class 則表示方法無參數(shù)Object result = addMethod.invoke(invokeTester, new Object

15、 new Integer(100, new Integer(200 ;System.out.println(Integer result;/ 調(diào)用InvokeTester對象的echo(方法Method echoMethod = classType.getMethod("echo",new Class String.class ;result = echoMethod.invoke(invokeTester, new Object "Hello" ;System.out.println(String result;Output:300echo: Hell

16、oB. 針對具有帶參數(shù)構(gòu)造函數(shù)的類:Java 允許一個(gè)類擁有多個(gè)構(gòu)造方法,并且可以在構(gòu)造對象時(shí)傳入?yún)?shù)用來初始化對象。比如 Employee 對象的構(gòu)造方法需要一個(gè) String 的參數(shù),用來告訴其 id 。public class Employee private String id; public Employee(String id this.id = id;在利用反射創(chuàng)建 Employee 對象時(shí),我們必需為其創(chuàng)建 Constructor 對象,用來反映此 Class 對象所表示的類或接口的指定構(gòu)造方法。/ 生成cl

17、assClass cls = Class.forName(className;/ 參數(shù)類型Class types = new Class String.class / 類數(shù)值對象Object values = new Object  "001" / 找到指定的構(gòu)造方法Constructor constructor = cls.getDeclaredConstructor(types;/ 設(shè)置安全檢查,訪問私有構(gòu)造函數(shù)constructor.setAccessible(true;/ 創(chuàng)建對象Employee

18、 e = (Employee constructor.newInstance(values;自己的例子:import java.lang.reflect.Constructor;import java.lang.reflect.Method;/* 運(yùn)用反射機(jī)制調(diào)一個(gè)類的方法*/public class InvokeTester int i;String s;public InvokeTester(int i, String sthis.i = i;this.s = s;public int add(int param1, int param2 return param1 + param2;pu

19、blic String echo(String msg return "echo: " + msg;public static void main(String args throws Exception Class classType = InvokeTester.class;/ 獲得該類的一個(gè)默認(rèn)對象(實(shí)例),與方法Object objectCopy=classType.getConstructor(new/ Class.newInstance(new Object;/ 等效/ Object invokeTester = classType.newInstance(;

20、/注意是默認(rèn)對象,即InvokeTester類只有默認(rèn)構(gòu)造函數(shù)/ Object invokeTester = classType.getConstructor(new Class int.class, String.class ;/ 構(gòu)造函數(shù)參數(shù)類型Class types = new Class int.class, String.class;/ 構(gòu)造函數(shù)參數(shù)值Object values = new Object 12, "abc"/找到指定的構(gòu)造方法Constructor constructor = classType.getDeclaredConstructor(typ

21、es;/設(shè)置安全檢查,訪問私有構(gòu)造函數(shù)constructor.setAccessible(true;/創(chuàng)建對象InvokeTester invokeTester = (InvokeTester constructor.newInstance(values;/ 調(diào)用InvokeTester對象的add(方法Method addMethod = classType.getMethod("add", new Class int.class,int.class ; /如果第二個(gè)參數(shù)為 new Class 則表示方法無參數(shù)Object result = addMethod.invok

22、e(invokeTester, new Object new Integer(100, new Integer(200 ;System.out.println(Integer result;/ 調(diào)用InvokeTester對象的echo(方法Method echoMethod = classType.getMethod("echo",new Class String.class ;result = echoMethod.invoke(invokeTester, new Object "Hello" ;System.out.println(String r

23、esult;Output:300echo: Hello7. 在生成對象后直接訪問屬性:import java.lang.reflect.Field;public class TestField  public String test;public static void main(String args  try         Class cls = Class.forName("TestField"Field f = cls.getField("test"TestField tf = (TestFieldcls.newInstance(; f.set(tf, "abc"System.out.println(tf.test; catch (Exception ex System.out.println(ex;  8. 獲取Field值:a. b. Etc.import java.lang.reflect.*;import java.awt.*;class Sam

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論