类的类getDeclaredMethod()方法 (Class class getDeclaredMethod() method)
getDeclaredMethod() method is available in java.lang package.
getDeclaredMethod()方法在java.lang包中可用。
getDeclaredMethod() method is used to return Method objects that indicate the given declared method of the class or an interface denoted by this Class object.
getDeclaredMethod()方法用于返回Method对象,这些对象指示给定的类的声明方法或由此Class对象表示的接口。
getDeclaredMethod() method is a non-static method, it is accessible with the class objects only and if we try to access the method with the class name then we will get an error.
getDeclaredMethod()方法是一个非静态方法,只能使用类对象进行访问,如果尝试使用类名称访问该方法,则会收到错误消息。
getDeclaredMethod() method accepts two arguments, the first argument is of "String" type and the second argument is an array of "Class" type.
getDeclaredMethod()方法接受两个参数,第一个参数为“字符串”类型,第二个参数为“类”类型的数组。
getDeclaredMethod() method may throw an exception at the time of returning a Method object.
getDeclaredMethod()方法在返回Method对象时可能会引发异常。
- NoSuchMethodException: In this exception when a specifying method does not exist.NoSuchMethodException :在此异常中,当指定方法不存在时。
- SecurityException: In this exception, it may raise when the security manager exists.SecurityException :在此异常中,当安全管理器存在时可能会引发此异常。
- NullPointerException: In this exception when the given Method name is null.NullPointerException :在此异常中,当给定的Method名称为null时。
Syntax:
句法:
public Method getDeclaredMethod (String method_name, Class ...paramType);
Parameter(s):
参数:
String method_name – represents the name of the method.
字符串method_name –表示方法的名称。
Class ...paramType – represents the parameter array of "Class" type.
Class ... paramType –表示“类”类型的参数数组。
Return value:
返回值:
The return type of this method is Method, it returns the Method object for the method of this Class meets the given method_name and parameter array paramType.
该方法的返回类型为Method ,它返回该Class方法满足给定method_name和参数数组paramType的Method对象。
Example:
例:
// Java program to demonstrate the example of Method
// getDeclaredMethod (String method_name, Class ...paramType)
// method of Class
import java.lang.reflect.*;
public class GetDeclaredMethodOfClass {
public static void main(String[] args) throws Exception {
String str = new String();
GetDeclaredMethodOfClass dc = new GetDeclaredMethodOfClass();
// Get Class object of String
Class cl = str.getClass();
// Get Class object of GetDeclaredMethodOfClass
Class dm = dc.getClass();
// Calling No argument Method
Method no_argument_method = cl.getDeclaredMethod("length", null);
System.out.println(" String Method = " + no_argument_method.toString());
Class[] method_arguments = new Class[2];
method_arguments[0] = Integer.class;
method_arguments[1] = Float.class;
// Calling argument Method
Method argument_method = dm.getDeclaredMethod("argumentMethod", method_arguments);
System.out.println("This Class Method = " + argument_method.toString());
}
public void argumentMethod(Integer i, Float f) {
this.i = i;
this.f = f;
}
public int i = 10;
private float f = 10.2f;
}
Output
输出量
String Method = public int java.lang.String.length()
This Class Method = public void GetDeclaredMethodOfClass.argumentMethod(java.lang.Integer,java.lang.Float)
翻译自: https://www.includehelp.com/java/class-class-getdeclaredmethod-method-with-example.aspx