类类getDeclaredConstructors()方法 (Class class getDeclaredConstructors() method)
getDeclaredConstructors() method is available in java.lang package.
getDeclaredConstructors()方法在java.lang包中可用。
getDeclaredConstructors() method is used to return an array of Constructor object that indicates the types of constructor defined by the class denoted by this Class object (Constructor may be public, private, protected or default).
getDeclaredConstructors()方法用于返回一个Constructor对象数组,该数组指示此Class对象所表示的类定义的构造函数的类型(Constructor可以是public,private,protected或default)。
getDeclaredConstructors() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
getDeclaredConstructors()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
getDeclaredConstructors() method may throw an exception at the time of returning a Constructor[].
getDeclaredConstructors()方法在返回Constructor []时可能会引发异常。
SecurityException: In this exception, it may raise when the security manager exists.
SecurityException :在此异常中,当安全管理器存在时可能会引发此异常。
Syntax:
句法:
public Constructor[] getDeclaredConstructors ();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of this method is Constructor[], it returns an array of Constructor object denoting all declared constructor (private, public, protected, default) of this Class.
此方法的返回类型为Constructor [] ,它返回一个Constructor对象数组,该数组表示该Class的所有已声明的构造函数(私有,公共,保护,默认)。
Example:
例:
// Java program to demonstrate the example
// of Constructor [] getDeclaredConstructors() method of Class
import java.lang.reflect.*;
public class GetDeclaredConstructorsOfClass {
public static void main(String[] args) throws Exception {
GetDeclaredConstructorsOfClass dc = new GetDeclaredConstructorsOfClass();
Class cl = dc.getClass();
Constructor[] cons = cl.getDeclaredConstructors();
for (int i = 0; i < cons.length; ++i)
System.out.println("Declared Constructors :" + cons[i].toString());
}
private GetDeclaredConstructorsOfClass(Integer i, Short s, Long l) {
this.i = i;
this.s = s;
this.l = l;
}
public GetDeclaredConstructorsOfClass() {
System.out.println("We are in public Constructor");
}
short sh = 10;
Integer i = new Integer(100);
Short s = new Short(sh);
Long l = new Long(30l);
}
Output
输出量
We are in public Constructor
Declared Constructors :private GetDeclaredConstructorsOfClass(java.lang.Integer,java.lang.Short,java.lang.Long)
Declared Constructors :public GetDeclaredConstructorsOfClass()
翻译自: https://www.includehelp.com/java/class-class-getdeclaredconstructors-method-with-example.aspx