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
广告
© . All rights reserved.