反射类,获取类的构造函数
public class Demo01Person { public String getName() { return name; } public Demo01Person(String name, Integer age) { this.name = name; this.age = age; } public Demo01Person() { } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getAge1() { return age1; } public void setAge1(Integer age1) { this.age1 = age1; } public Demo01Person(String name, Integer age, Integer age1) { this.name = name; this.age = age; this.age1 = age1; } private String name; private Integer age; public Integer age1; protected String d; }
public static void main(String[] args) throws Exception { Class personClass = Demo01Person.class; Constructor constructor = personClass.getConstructor(String.class, int.class); System.out.println(constructor); }
Exception in thread "main" java.lang.NoSuchMethodException: Demo.DemoReflect.Demo01Person.<init>(java.lang.String, int)
at java.base/java.lang.Class.getConstructor0(Class.java:3322)
at java.base/java.lang.Class.getConstructor(Class.java:2108)