更新時(shí)間:2022-09-23 17:10:50 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1105次
學(xué)習(xí)Java中的反射其實(shí)不難,反射是一種 API,用于在運(yùn)行時(shí)檢查或修改方法、類和接口的行為。反射所需的類在java.lang.reflect包下提供,這對(duì)于理解反射至關(guān)重要。所以我們用視覺輔助來(lái)說(shuō)明這個(gè)包,以便更好地理解如下:
反射可用于獲取有關(guān)類、構(gòu)造函數(shù)和方法的信息,如下表所示:
班級(jí) getClass() 方法用于獲取對(duì)象所屬的類的名稱。
構(gòu)造函數(shù)getConstructors() 方法用于獲取對(duì)象所屬類的公共構(gòu)造函數(shù)。
方法getMethods() 方法用于獲取對(duì)象所屬類的公共方法。
如果我們知道它的名稱和參數(shù)類型,我們可以通過(guò)反射調(diào)用方法。為此,我們使用如下所述的兩種方法,然后繼續(xù)進(jìn)行如下操作:
getDeclaredMethod()
調(diào)用()
方法一: getDeclaredMethod (): 創(chuàng)建要調(diào)用的方法的對(duì)象。
語(yǔ)法:此方法的語(yǔ)法
Class.getDeclaredMethod(名稱,參數(shù)類型)
參數(shù):
方法二: invoke():它在運(yùn)行時(shí)調(diào)用類的方法我們使用下面的方法。
句法:
Method.invoke(對(duì)象,參數(shù))
提示:如果類的方法不接受任何參數(shù),則將 null 作為參數(shù)傳遞。
注意:通過(guò)反射,我們可以借助類對(duì)象訪問(wèn)類的私有變量和方法,并使用上面討論的對(duì)象調(diào)用方法。為此,我們使用以下兩種方法。
方法三: Class.getDeclaredField(FieldName):用于獲取私有字段。返回指定字段名稱的 Field 類型的對(duì)象。
方法 4: Field.setAccessible(true): 允許訪問(wèn)該字段,而不考慮與該字段一起使用的訪問(wèn)修飾符。
從反射 API 得出的重要觀察結(jié)果
可擴(kuò)展性特性:應(yīng)用程序可以通過(guò)使用它們的完全限定名稱創(chuàng)建可擴(kuò)展性對(duì)象的實(shí)例來(lái)使用外部的、用戶定義的類。
調(diào)試和測(cè)試工具:調(diào)試器使用反射屬性來(lái)檢查類的私有成員。
性能開銷:反射操作的性能比非反射操作慢,在性能敏感的應(yīng)用程序中頻繁調(diào)用的代碼部分應(yīng)避免使用。
內(nèi)部暴露:反射代碼破壞了抽象,因此可能會(huì)隨著平臺(tái)的升級(jí)而改變行為。
例子
// Java Program to demonstrate the Use of Reflection
// Importing required classes
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
// Class 1
// Of Whose object is to be created
class Test {
// creating a private field
private String s;
// Constructor of this class
// Constructor 1
// Public constructor
public Test() { s = "GeeksforGeeks"; }
// Constructor 2
// no arguments
public void method1()
{
System.out.println("The string is " + s);
}
// Constructor 3
// int as argument
public void method2(int n)
{
System.out.println("The number is " + n);
}
// Constructor 4
// Private method
private void method3()
{
System.out.println("Private method invoked");
}
}
// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String args[]) throws Exception
{
// Creating object whose property is to be checked
// Creating an object of class 1 inside main()
// method
Test obj = new Test();
// Creating class object from the object using
// getClass() method
Class cls = obj.getClass();
// Printing the name of class
// using getName() method
System.out.println("The name of class is "
+ cls.getName());
// Getting the constructor of the class through the
// object of the class
Constructor constructor = cls.getConstructor();
// Printing the name of constructor
// using getName() method
System.out.println("The name of constructor is "
+ constructor.getName());
// Display message only
System.out.println(
"The public methods of class are : ");
// Getting methods of the class through the object
// of the class by using getMethods
Method[] methods = cls.getMethods();
// Printing method names
for (Method method : methods)
System.out.println(method.getName());
// Creates object of desired method by
// providing the method name and parameter class as
// arguments to the getDeclaredMethod() method
Method methodcall1
= cls.getDeclaredMethod("method2", int.class);
// Invoking the method at runtime
methodcall1.invoke(obj, 19);
// Creates object of the desired field by
// providing the name of field as argument to the
// getDeclaredField() method
Field field = cls.getDeclaredField("s");
// Allows the object to access the field
// irrespective of the access specifier used with
// the field
field.setAccessible(true);
// Takes object and the new value to be assigned
// to the field as arguments
field.set(obj, "JAVA");
// Creates object of desired method by providing the
// method name as argument to the
// getDeclaredMethod()
Method methodcall2
= cls.getDeclaredMethod("method1");
// Invokes the method at runtime
methodcall2.invoke(obj);
// Creates object of the desired method by providing
// the name of method as argument to the
// getDeclaredMethod() method
Method methodcall3
= cls.getDeclaredMethod("method3");
// Allows the object to access the method
// irrespective of the access specifier used with
// the method
methodcall3.setAccessible(true);
// Invoking the method at runtime
methodcall3.invoke(obj);
}
}
輸出:
以上就是動(dòng)力節(jié)點(diǎn)小編介紹的"Java反射學(xué)習(xí),要點(diǎn)你抓好了嗎",希望對(duì)大家有幫助,如有疑問(wèn),請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為您務(wù)。
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743