Java Class getMethod() 方法



描述

Java Class getMethod() 方法返回一個 Method 物件,該物件反映由該 Class 物件表示的類或介面的指定公共成員方法。name 引數是一個字串,指定所需方法的簡單名稱。

parameterTypes 引數是一個 Class 物件陣列,按宣告順序標識方法的形式引數型別。如果 parameterTypes 為 null,則將其視為空陣列。

宣告

以下是 java.lang.Class.getMethod() 方法的宣告

public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException

引數

  • name − 這是方法的名稱。

  • parameterTypes − 這是引數列表。

返回值

此方法返回與指定的名稱和 parameterTypes 匹配的 Method 物件。

異常

  • NoSuchMethodException − 如果找不到匹配的方法,或者名稱為 <init> 或 <clinit>。

  • NullPointerException − 如果 name 為 null

  • SecurityException − 如果存在安全管理器 s。

獲取類的 Method 示例

以下示例顯示了 java.lang.Class.getMethod() 方法的用法。在這個程式中,我們建立了一個 ClassDemo 的例項,然後使用 getClass() 方法檢索例項的類。使用 getMethod(),我們檢索了所需的方法並列印它。

package com.tutorialspoint;

import java.lang.reflect.Method;

public class ClassDemo {

   public static void main(String[] args) {
    
      ClassDemo cls = new ClassDemo();
      Class c = cls.getClass();

      try {                
         // parameter type is null
         Method m = c.getMethod("show", null);
         System.out.println("method = " + m.toString());        
      } catch(NoSuchMethodException e) {
         System.out.println(e.toString());
      }
 
      try {
         // method Long
         Class[] cArg = new Class[1];
         cArg[0] = Long.class;
         Method lMethod = c.getMethod("showLong", cArg);
         System.out.println("method = " + lMethod.toString());
      } catch(NoSuchMethodException e) {
         System.out.println(e.toString());
      }
   }

   public Integer show() {
      return 1;
   }
    
   public void showLong(Long l) {
      this.l = l;
   }
   long l = 78655;
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

method = public java.lang.Integer com.tutorialspoint.ClassDemo.show()
method = public void com.tutorialspoint.ClassDemo.showLong(java.lang.Long)

獲取 ArrayList 的 Method 示例

以下示例顯示了 java.lang.Class.getMethod() 方法的用法。在這個程式中,我們使用了 ArrayList 的類。使用 getMethod(),我們檢索了所需的方法並列印它。

package com.tutorialspoint;

import java.lang.reflect.Method;
import java.util.ArrayList;

public class ClassDemo {

   public static void main(String[] args) {
   
      Class c = ArrayList.class;

      try {                
         // parameter type is null
         Method m = c.getMethod("size", null);
         System.out.println("method = " + m.toString());        
      } catch(NoSuchMethodException e) {
         System.out.println(e.toString());
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

method = public int java.util.ArrayList.size()

獲取 Thread 的 Method 示例

以下示例顯示了 java.lang.Class.getMethod() 方法的用法。在這個程式中,我們使用了 Thread 的類。使用 getMethod(),我們檢索了所需的方法並列印它。

package com.tutorialspoint;

import java.lang.reflect.Method;

public class ClassDemo {

   public static void main(String[] args) {
   
      Class c = Thread.class;

      try {                
         // parameter type is null
         Method m = c.getMethod("start", null);
         System.out.println("method = " + m.toString());        
      } catch(NoSuchMethodException e) {
         System.out.println(e.toString());
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

method = public void java.lang.Thread.start()
java_lang_class.htm
廣告