首頁 > Java > java教程 > 主體

Java getMethod()

PHPz
發布: 2024-08-30 15:39:49
原創
1032 人瀏覽過

Java getMethod() 是java.lang.Class.getMethod() 中的一個方法,它傳回java.lang.reflect 套件中Method 類別的實例,該實例保存給定Class 物件參考中存在的給定公共成員函數的引用到一個類別或介面。此方法將需要傳遞的方法的名稱作為其第一個參數。要傳遞的第二個參數是 Class 物件數組,用於確定傳回方法的形式參數資料類型,或是空數組,用於確定 paramterTypes 為 null。這裡使用的搜尋演算法與私人 GetPublicMethods() 方法相同。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

getMethod() 拋出 3 種類型的例外,如下:-

  1. NoSuchMethodException
  2. 空指標異常
  3. 安全異常

文法

下面是java.lang.Class的getMethod的簽章

public Method getMethod(String name, Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException
登入後複製
  • public:此關鍵字決定給定的方法可以從項目中的任何類別存取。
  • 傳回類型方法:此方法傳回 Method 類別的實例,該實例引用其名稱已作為參數傳遞的所需方法。
  • 參數:
  • Name 此參數指引用類別或介面物件中存在的方法名稱的字串表示形式。如果類別中不存在此類方法,則會引發 NoSuchMethodException。否則,演算法運行,並返回該方法。
  • parameterTypes:這是指類別類型物件的數組,該數組指向名稱參數中的方法需要作為參數的資料。此陣列的大小取決於指定方法名稱所需的參數。如果該方法不需要參數,則將 null 傳遞給該參數。

範例

如果我們有一個如下所示的類別示範:

class Demo{
public void method1(String a){
System.out.println(a);
}
}
登入後複製

然後呼叫 getMethod 就會像:

Demo demoObj= new Demo();// Object of Demo class
Class cObj= demoObj.getClass()
Class [] carr = new Class[1];
carr[0] = String.class;// class reference to java.lang.String class stored In the array of type Class
Method mObj = cObj.getMethod("method1",carr);
登入後複製

getMethod() 在 Java 中如何運作?

getMethod() 傳回引用類別或介面物件中指定方法的 Method 實例。

  • 它採用 String 資料類型的 name 參數,該參數保存需要在指定類別或介面中尋找的公共方法的名稱。它還需要一個類別物件數組,代表我們正在尋找的函數的參數類型。
  • JVM 讀取兩個參數並執行與 java.lang.Class 中的 privateGetPublicMethods() 方法相同的搜尋演算法,並搜尋指定名稱的公共方法是否存在。如果類別中存在多個方法,則傳回具有更具體傳回類型的方法。否則,方法是任意選擇的。

如果它找到該方法,它會傳回一個保存其引用的方法類別實例。

如果指定的方法不需要任何參數,則傳遞 null 來取代parameterType。這在方法重載的情況下很有幫助,在這種情況下,我們有多個具有相同名稱但參數數量或資料類型不同的方法。此方法會拋出 3 種類型的例外:-

1。 NoSuchMethodException:當 JVM 無法在類別或介面中找到任何具有指定名稱的方法時,請拋出此類例外。

2。 SecurityException:當

時拋出此類異常
  • checkMemberAccess(this, Member.PUBLIC) 被調用,拒絕對其的存取。
  • 呼叫者類別載入器與目前類別的祖先類別的載入器不同;因此,SecurityManagers.checkPackageAccess() 被呼叫;因此,對套件的存取被拒絕。

3。 NullPointerException:如果傳遞 null 來取代參數中的方法名稱,則會拋出此例外。

實作 Java getMethod() 的範例

以下是提到的範例:

範例#1

在此範例中,我們將顯示對 Office 類別的兩個方法的 getMethod 呼叫的輸出,一個需要對象,另一個不需要參數。

//package Proc;
import java.lang.reflect.*;
class Office{
public String OfficeLocation() {
return location;
}
public String getEmpName(Integer eid) {
return"Sergio";
}
String location = "Bangalore";
}
public class prac1 {
public static void main(String[] args) {
Office ofc = new Office();
Class cObj = ofc.getClass();
Class[] carr = new Class[1];
carr[0] = Integer.class;
try {
Method meth = cObj.getMethod("OfficeLocation", null);
System.out.println("Method with specified name is = " + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
try {
Method meth = cObj.getMethod("getEmpName", carr);
System.out.println("Method with specified name is = " + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
}
}
登入後複製

輸出:

Java getMethod()

範例#2

在這個範例中,我們將看看 JVM 是否能夠找到具有給定名稱的類別的私有方法。

//package Proc;
import java.lang.reflect.*;
public class prac1 {
public static void main(String[] args) {
Office ofc = new Office();
Class cObj = ofc.getClass();
try {
Method meth = cObj.getMethod("OfficeLocation", null);
System.out.println("Method with specified name is = " + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
}
}
class Office{
private String OfficeLocation() {
return location;
}
public String getEmpName(Integer eid) {
return "Sergio";
}
String location = "Bangalore";
}
登入後複製

輸出:

Java getMethod()

Example #3

In this example , we will see how different exceptions occur when a non-existing method is called, and null is passed in the method’s name.

//package Proc;
import java.lang.reflect.*;
class Office{
public String OfficeLocation() {
return location;
}
public String getEmpName(Integer eid) {
return "Sergio";
}
String location = "Bangalore";
}
public class prac1 {
public static void main(String[] args) {
Office ofc = new Office();
Class cObj = ofc.getClass();
Class[] carr = new Class[1];
carr[0] = Integer.class;
try {
Method meth = cObj.getMethod("show", null);
System.out.println("Method found " + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
try {
Method meth = cObj.getMethod(null, carr);
System.out.println("Method found" + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}catch(NullPointerException e) {
System.out.println(e.toString());
}
}
}
登入後複製

Output:

Java getMethod()

Conclusion

Java.lang.getMethod() is a method used to search if a method with the given name and type of arguments is present in the class or not. It uses the same algorithm to find the method used in the privateGetPublicMethods() method. JVM search for the given public method and returns a Method instance; otherwise, NoSuchMethodException is raised.

以上是Java getMethod()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!