我是靠谱客的博主 迷你期待,这篇文章主要介绍java.lang.Class.getDeclaredConstructor()方法实例,现在分享给大家,希望可以做个参考。

java.lang.Class.getDeclaredConstructor() 方法返回一个Constructor对象,它反映此Class对象所表示的类或接口指定的构造函数。parameterTypesparameter是确定构造函数的形参类型,在Class对象声明顺序的数组。

声明

以下是java.lang.Class.getDeclaredConstructor()方法的声明

复制代码
1
publicConstructor<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
30
import 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
2
no argument constructor Constructor=publicClassDemo(java.lang.Double,java.lang.Long)

 

最后

以上就是迷你期待最近收集整理的关于java.lang.Class.getDeclaredConstructor()方法实例的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部