Java.lang.Class.getConstructors() 方法
描述
java.lang.Class.getConstructors() 方法返回一个包含 Constructor 对象的数组,该数组反映了该 Class 对象表示的类的所有公共构造函数。 如果类没有公共构造函数,或者类是数组类,或者类反映原始类型或 void,则返回长度为 0 的数组。
声明
以下是 java.lang.Class.getConstructors() 方法的声明。
public Constructor<?>[] getConstructors() throws SecurityException
参数
NA
返回值
此方法返回表示此类的公共构造函数的 Constructor 对象数组。
异常
SecurityException − 如果存在安全管理员 s。
示例
下面的例子展示了 java.lang.Class.getConstructors() 方法的使用。
package com.tutorialspoint;
import java.lang.reflect.*;
public class ClassDemo {
public static void main(String[] args) {
try {
Class cls = Class.forName("java.awt.Panel");
System.out.println("Panel Constructors =");
/* returns the array of Constructor objects representing the public
constructors of this class */
Constructor c[] = cls.getConstructors();
for(int i = 0; i < c.length; i++) {
System.out.println(c[i]);
}
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Panel Constructors = public java.awt.Panel() public java.awt.Panel(java.awt.LayoutManager)

