本文主要是介绍java.lang.Class.getConstructor(Class[] parameterTypes),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Gets the public constructor that takes the given arguments.
public java.lang.reflect.Constructor getConstructor(java.lang.Class[ ] parameterTypes)
Parameters
Parameter | Description |
---|---|
parameterTypes | An array of bf6c9478-b86e-41e1-b8c9-48372912df0a objects representing the arguments to the constructor. A 5c106966-ba6e-4e68-8ed8-e4131d1bebff is thrown if there is no constructor with the given parameter types or if the constructor does not have public access. |
// class_getconstructor.jslimport java.lang.reflect.Constructor;public class Program {public static void main(String[] args){try{// Get the Class object associated with this class.Program program = new Program();Class progClass = program.getClass();// Find the constructor that only takes a String.Class[] ctorArgs1 = new Class[1];ctorArgs1[0] = String.class;Constructor strCtor = progClass.getConstructor(ctorArgs1);System.out.println("Constructor found: " +strCtor.toString());// Find the constructor that takes a String and an int.Class[] ctorArgs2 = new Class[2];ctorArgs2[0] = String.class;ctorArgs2[1] = Integer.class;Constructor strIntCtor = progClass.getConstructor(ctorArgs2);System.out.println("Constructor found: " +strIntCtor.toString());}catch (NoSuchMethodException ex){System.out.println("Constructor doesn't exist or is " +"not public: " + ex.toString());}}public Program(){}public Program(String str){this.str = str;}private Program(String str, Integer i){this.str = str;this.i = i;}private String str = "Hello";private Integer i = new Integer(0); }/* Output: Constructor found: public Program(java.lang.String) Constructor doesn't exist or is not public: java.lang.NoSuchMethodException */
这篇关于java.lang.Class.getConstructor(Class[] parameterTypes)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!