知道类的名字,如何调用她的有参构造函数(不用new) 看看java的反射机制和java.lang.class和java.lang.reflect包Constructor[] constructors = c.getDeclaredConstructors(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public Function(String function,Object[] arg) throws Exception { sFunction = function; int i = function.lastIndexOf('.'); String classname = "com.justnorth.bc.report.functions."+function; String methodname ="getValue"; Class classinstance = null; try { classinstance = Class.forName(classname); } catch (Exception e) { throw e; } Class[] parameterTypes=new Class[arg.length]; for(i=0;i<parameterTypes.length;i++){ parameterTypes[i]=arg[i].getClass(); System.out.println(parameterTypes[i]); } //测试传过来的雷的构造函数的参数类型 // Constructor cs[]=classinstance.getDeclaredConstructors() ;// for(int j=0;j<cs.length;j++){// Class[] p=cs[j].getParameterTypes();// for(i=0;i<p.length;i++){// System.out.println(p[i]);// }// } Constructor construct=classinstance.getConstructor(parameterTypes); functionInstance=construct.newInstance(arg); mMethod=classinstance.getMethod(methodname,new Class[0]); } A 调用B1.A中 B test =new B("参数1","参数2")2.B中 你怎么接都可以底!!! 问题就处在构造方法的参数是基本类型。我们知道Class.getConstructor(Class[])和Constructor.newInstance(Object[]),所以你直接传入int[]是不行的。关于这样有基本类型的构造我们这样做(使用基本类型对应类中的static TYPE): Class[] paramTypes = {Integer.TYPE, Integer.TYPE}; Object[] params = {new Integer(i), new Integer(j)}; Constructor con = Class.forName(className).getConstructor(paramTypes); CCC c = (CCC)con.newInstance(params);这样就解决了。 你的参数是哪个类的,就Class.forName("哪个类"); 类名:com.aaa.CCC,已知其构造函数CCC(int,int)String className = "com.aaa.CCC";Class[] param = {Integer.TYPE, Integer.TYPE};int[] temp = {1, 2};CCC c = (CCC)CCC.class.getConstructor(param).newInstance(temp); 一些Java面试题,求答案 java实现一个静态方法传递一个数组参数返回一个大小倍增的数组并包含原来数组中的对象 请问CRC8\16\32之间的区别是什么? 关于notify的问题 急请教:图片显示问题! 如何获取某个double类型数据的指定精度的值 关于安装netbeans的问题 做电力软件的同胞请进来拿分 对象持久化是个什么概念? 求大神解释一下java中的break label;用法的优缺点 教教我怎么配置jdk1.3.1_07 关于JAVA文件执行时出现的错误
sFunction = function;
int i = function.lastIndexOf('.'); String classname = "com.justnorth.bc.report.functions."+function; String methodname ="getValue";
Class classinstance = null;
try {
classinstance = Class.forName(classname);
} catch (Exception e) {
throw e;
}
Class[] parameterTypes=new Class[arg.length];
for(i=0;i<parameterTypes.length;i++){
parameterTypes[i]=arg[i].getClass();
System.out.println(parameterTypes[i]);
}
//测试传过来的雷的构造函数的参数类型
// Constructor cs[]=classinstance.getDeclaredConstructors() ;
// for(int j=0;j<cs.length;j++){
// Class[] p=cs[j].getParameterTypes();
// for(i=0;i<p.length;i++){
// System.out.println(p[i]);
// }
// }
Constructor construct=classinstance.getConstructor(parameterTypes);
functionInstance=construct.newInstance(arg);
mMethod=classinstance.getMethod(methodname,new Class[0]);
}
1.A中
B test =new B("参数1","参数2")
2.B中
你怎么接都可以底!!!
我们知道Class.getConstructor(Class[])和Constructor.newInstance(Object[]),所以你直接传入int[]是不行的。关于这样有基本类型的构造我们这样做(使用基本类型对应类中的static TYPE): Class[] paramTypes = {Integer.TYPE, Integer.TYPE};
Object[] params = {new Integer(i), new Integer(j)};
Constructor con = Class.forName(className).getConstructor(paramTypes);
CCC c = (CCC)con.newInstance(params);这样就解决了。
Class[] param = {Integer.TYPE, Integer.TYPE};
int[] temp = {1, 2};
CCC c = (CCC)CCC.class.getConstructor(param).newInstance(temp);