[code]import java.lang.reflect.*;
public class TestConstructor {
public static void main(String[] args) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Constructor[] con=dog.class.getConstructors();
for(Constructor conn:con){
System.out.println(conn);
}
dog a=dog.class.getConstructor(String.class,int.class).newInstance("My pretty little dog",5);
}
}
class dog {
public dog(){}
public dog(String dogage){}
public dog(String dogname, int dogage) {
System.out.println(dogname+" was "+dogage+" years old~.. haha");
}
public dog(int dogage){}
}[/code]就是简单的得到构造方法,然后在执行实例化的操作呗
打印出来的结果是
public dog()
public dog(java.lang.String)
public dog(java.lang.String,int)
public dog(int)
My pretty little dog was 5 years old~.. haha |