大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國(guó)咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) 常見問(wèn)題 Java反射學(xué)習(xí),要點(diǎn)你抓好了嗎

Java反射學(xué)習(xí),要點(diǎn)你抓好了嗎

更新時(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è)包,以便更好地理解如下:

java反射學(xué)習(xí)

  • 反射為我們提供了有關(guān)對(duì)象所屬的類的信息,以及可以使用該對(duì)象執(zhí)行的該類的方法。
  • 通過(guò)反射,我們可以在運(yùn)行時(shí)調(diào)用方法,而不管與它們一起使用的訪問(wèn)說(shuō)明符。

java反射學(xué)習(xí)

反射可用于獲取有關(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ù):

  • 要?jiǎng)?chuàng)建其對(duì)象的方法的名稱
  • 類對(duì)象數(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);
	}
}

輸出:

java反射學(xué)習(xí)

以上就是動(dòng)力節(jié)點(diǎn)小編介紹的"Java反射學(xué)習(xí),要點(diǎn)你抓好了嗎",希望對(duì)大家有幫助,如有疑問(wèn),請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為您務(wù)。

提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)

  • 全國(guó)校區(qū) 2025-04-24 搶座中
  • 全國(guó)校區(qū) 2025-05-15 搶座中
  • 全國(guó)校區(qū) 2025-06-05 搶座中
  • 全國(guó)校區(qū) 2025-06-26 搶座中
免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: h视频在线观看免费网站 | 天天综合天天做天天综合 | 欧美伊香蕉久久综合类网站 | 香蕉久久成人网 | 久久精品视频免费 | 国产在线精品一区二区 | 国产一区二区三区视频在线观看 | 欧美深度肠交 | 中文字幕视频在线观看 | 在线97| 久久国产欧美日韩精品 | 免费看真人a一级毛片 | 久久精品国产波多野结衣 | 日本不卡一二三区 | 国产成人免费 | 一级女人18毛片免费 | www.xxxx欧美| 欧美爱爱片 | 欧美国产日韩一区二区三区 | 在线视频一区二区日韩国产 | 国产精品永久免费视频 | 欧美日韩免费大片 | 国产 magnet | 超91视频 | 九九这里有精品 | 日本aaaa毛片在线看 | 成人免费观看网站 | 免费高清a毛片 | 一级全黄色毛片 | 天天爽夜夜爽精品视频一 | 亚洲欧美日韩精品久久亚洲区 | 久久成人免费观看草草影院 | 中文字幕日韩欧美 | 成人午夜大片免费视频77777 | 亚洲 欧美 中文字幕 | 这里只有精品视频 | 99re国产精品视频首页 | 四虎影视国产精品亚洲精品hd | 色姝影院免费 | 91精品成人免费国产 | 西西做人爱免费视频 |