军绿色陈列搭什么颜色:运行时生成instances中带参数ctor

来源:百度文库 编辑:高考问答 时间:2024/05/15 04:07:09
#001 Class c = Class.forName("DynTest");

#002 Class[] pTypes = new Class[] { double.class, int.class };

#003 Constructor ctor = c.getConstructor(pTypes);

#004 //指定parameter list,便可获得特定之ctor

#005

#006 Object obj = null;

#007 Object[] arg = new Object[] {3.14159, 125}; //自变量

#008 obj = ctor.newInstance(arg);

#009 System.out.println(obj);
有一个问题,为什么DynTest中的这个构造器必须是public?否则就抛出异常?NoSuchMethodException?多谢各位大侠相助~~~~久等~~~~