java.lang.Class.getConstructor()%uA0方法返回一个Constructor对象,它反映此Class对象所表示的类指定公共构造函数。parameterTypes参数是确定构造函数的形参类型,在声明的顺序Class对象的数组。
声明
以下是java.lang.Class.getConstructor()方法的声明
public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
参数
-
parameterTypes%uA0-- 这是参数数组。
返回值
此方法返回的公共构造匹配指定parameterTypes的构造函数对象。
异常
-
NoSuchMethodException%uA0-- 如果没有找到匹配的方法。
-
SecurityException%uA0-- 如果安全管理存在。
例子
下面的例子显示java.lang.Class.getConstructor()方法的使用。
package com.yiibai 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)