Java 類 getDeclaredMethod() 方法



描述

Java 類 getDeclaredMethod() 方法返回一個 Method 物件,該物件反映了由這個 Class 物件表示的類或介面的指定宣告方法。name 引數是一個字串,指定所需方法的簡單名稱;parameterTypes 引數是一個 Class 物件陣列,按宣告順序標識方法的形式引數型別。

宣告

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

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

引數

  • name − 這是方法的名稱。

  • parameterTypes − 這是引數陣列。

返回值

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

異常

  • NoSuchMethodException − 如果找不到匹配的方法。

  • NullPointerException − 如果 name 為 null。

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

獲取類的宣告方法示例

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

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.getDeclaredMethod("show", null);
         System.out.println("method = " + m.toString()); 

         // method Integer
         Class[] cArg = new Class[1];
         cArg[0] = Integer.class;
         Method lMethod = c.getDeclaredMethod("showInteger", cArg);
         System.out.println("method = " + lMethod.toString());
      } catch(NoSuchMethodException e) {
         System.out.println(e.toString());
      }
   }

   private Integer show() {
      return 1;
   }
    
   public void showInteger(Integer i) {
      this.i = i;
   }
   public int i = 78655;
}

輸出

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

method = private java.lang.Integer com.tutorialspoint.ClassDemo.show()
method = public void com.tutorialspoint.ClassDemo.showInteger(java.lang.Integer)

獲取 ArrayList 的宣告方法示例

以下示例演示了 java.lang.Class.getDeclaredMethod() 方法的用法。在這個程式中,我們使用了 ArrayList 的類。使用 getDeclaredMethod(),我們檢索了所需的方法並打印出來。

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.getDeclaredMethod("size", null);
         System.out.println("method = " + m.toString()); 
      } catch(NoSuchMethodException e) {
         System.out.println(e.toString());
      }
   }
}

輸出

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

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

獲取 Thread 的宣告方法示例

以下示例演示了 java.lang.Class.getDeclaredMethod() 方法的用法。在這個程式中,我們使用了 Thread 的類。使用 getDeclaredMethod(),我們檢索了所需的方法並打印出來。

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.getDeclaredMethod("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
廣告