Java通過反射獲取帶引數構造方法並使用

NO IMAGE
package com.sanmao10;
import java.lang.reflect.Constructor;
public class test2 {
/**
* ###27.05_反射(通過反射獲取帶參構造方法並使用)
* Constructor
* Class類的newInstance()方法是使用該類無參的建構函式建立物件, 如果一個類沒有無參的建構函式,
* 就不能這樣建立了,可以呼叫Class類的getConstructor(String.class,int.class)
* 方法獲取一個指定的建構函式然後再呼叫Constructor類的newInstance("張三",20)方法建立物件*/
public static void main(String[] args) throws Exception{
Class clazz=Class.forName("com.sanmao10.Person");
//        Person p=(Person)clazz.newInstance();    //通過無參構造建立物件
//        p.say();
Constructor c=clazz.getConstructor(String.class,int.class);//獲取有參構造
Person p=(Person) c.newInstance("sanmao",12);    //通過有參構造建立物件
p.say();
}
}
package com.sanmao10;
public class Person {
private String name;
private int age;
//    public Person() {
//    }
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (age != person.age) return false;
return name != null ? name.equals(person.name) : person.name == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result   age;
return result;
}
@Override
public String toString() {
return super.toString();
}
public void say(){
System.out.println(name "..." age);
}
public void papapa(){
System.out.println(name "   pa pa pa");
}
}