java.lang.Class.getDeclaredConstructor() 方法返回一个Constructor对象,它反映此Class对象所表示的类或接口指定的构造函数。parameterTypesparameter是确定构造函数的形参类型,在Class对象声明顺序的数组。
声明
以下是java.lang.Class.getDeclaredConstructor()方法的声明
复制代码
1publicConstructor<T> getDeclaredConstructor(Class<?>... parameterTypes)throwsNoSuchMethodException,SecurityException
参数
-
parameterTypes -- 这是参数数组。
返回值
此方法返回具有指定参数列表构造函数的构造函数对象。
异常
-
NoSuchMethodException -- 如果没有找到匹配的方法。
-
SecurityException --如果安全管理存在。
例子
下面的例子显示java.lang.Class.getDeclaredConstructor()方法的使用。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30import java.lang.reflect.*; public class ClassDemo { public static void main(String[] args) { try { ClassDemo cls = new ClassDemo(); Class c = cls.getClass(); // constructor with arguments as Double and Long Class[] cArg = new Class[2]; cArg[0] = Double.class; cArg[1] = Long.class; Constructor ct = c.getDeclaredConstructor(cArg); System.out.println("Constructor = " + ct.toString()); } catch(NoSuchMethodException e) { System.out.println(e.toString()); } catch(SecurityException e) { System.out.println(e.toString()); } } private ClassDemo() { System.out.println("no argument constructor"); } public ClassDemo(Double d, Long l) { this.d = d; this.l = l; } Double d = new Double(3.9d); Long l = new Long(7687); }
让我们来编译和运行上面的程序,这将产生以下结果:
复制代码
1
2no argument constructor Constructor=publicClassDemo(java.lang.Double,java.lang.Long)
最后
以上就是迷你期待最近收集整理的关于java.lang.Class.getDeclaredConstructor()方法实例的全部内容,更多相关java内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复