Constructor getConstructor(Class>... parameterTypes)
描述 (Description)
java.lang.Class.getConstructor()方法返回一个Constructor对象,该对象反映此Class对象所表示的类的指定公共构造函数。 parameterTypes参数是一个Class对象数组,它按声明的顺序标识构造函数的形式参数类型。
声明 (Declaration)
以下是java.lang.Class.getConstructor()方法的声明public Constructor getConstructor(Class>... parameterTypes)
throws NoSuchMethodException, SecurityException
参数 (Parameters)
parameterTypes - 这是参数数组。
返回值 (Return Value)
此方法返回与指定的parameterTypes匹配的公共构造函数的Constructor对象。
异常 (Exception)NoSuchMethodException - 如果找不到匹配的方法。
SecurityException - 如果存在安全管理器s。
例子 (Example)
以下示例显示了java.lang.Class.getConstructor()方法的用法。package com.iowiki;
import java.lang.reflect.*;
public class ClassDemo {
public static void main(String[] args) {
try {
// returns the Constructor object of the public constructor
Class cls[] = new Class[] { String.class };
Constructor c = String.class.getConstructor(cls);
System.out.println(c);
} catch(Exception e) {
System.out.println(e);
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -public java.lang.String(java.lang.String)