有一个叫A
public class A{
public A(String param1,String param2){
}
}
--------------
利用java反射机制,举例如下:
import java.lang.reflect.Constructor;
/**
*
*/
/**
* @author dennis
*
*/
public class Test3 {
private String owner;
private int i;
public Test3() {
}
/**
* @param owner
* @param i
*/
public Test3(String owner, int i) {
super();
this.owner = owner;
this.i = i;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public static void main(String args[]) throws Exception {
Class clazz = Class.forName("Test3");
Constructor constructor = clazz.getConstructor(String.class, int.class); //构造函数参数列表的class类型
Test3 test = (Test3) constructor.newInstance("test", 1); //传参
System.out.println(test.getOwner());
}
}