Class.forName(aa).newInstance 这样加载一个实现类,如果这个实现类有多个构造函数,是否都会调用呢? Class.forName(aa).newInstance 这样加载一个实现类,如果这个实现类有多个构造函数,是否都会调用呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Class.forName(aa).newInstance只会调用无参数的构造函数若没有 报异常 Just Try: public class TestClassforName { public TestClassforName() { System.out.println("Empty Paramter"); } public TestClassforName(int i) { System.out.println("Int Parameter"); } /** * @param args * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException { // TODO Auto-generated method stub Class.forName("TestClassforName").newInstance(); }}输出: Empty Paramter证明只会调用无参构造函数, 没有会报InstantiationException. 只会调用无参数构造函数;如果需要设置参数,可以通过set方法实现。 只会调用无参数构造函数;如果需要设置参数,可以通过set方法实现。 请问一下这个要怎么实现? 可以通过下述方法调用有参数的构造函数:import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;public class TestClassforName { public TestClassforName() { System.out.println("Empty Paramter"); } public TestClassforName(int i) { System.out.println("Int Parameter " + i); } /** * @param args * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException * @throws IllegalArgumentException */ public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { // TODO Auto-generated method stub Class c = Class.forName("TestClassforName"); Constructor cc = c.getConstructor(int.class); cc.newInstance(3);// .newInstance(); }} java学生管理系统 csdn 论坛的这个编辑器 调用applet 问题 关于构造方法 得到一个JScrollPane的对象,如果再get里面的数据 关于 字符串的动态截取问题! jsp用相对路径删除文件? 100分!求有关动画的程序代码。分不够另帖再加。 十万火急,那位知道如何用Properties从配置文件中把中文读入?? GUI 用paint方法画圆点问题 Student02370236(★★★★★★) 进来 求Java与模式电子版
public class TestClassforName { public TestClassforName() {
System.out.println("Empty Paramter");
}
public TestClassforName(int i) {
System.out.println("Int Parameter");
} /**
* @param args
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
// TODO Auto-generated method stub
Class.forName("TestClassforName").newInstance();
}}
输出: Empty Paramter
证明只会调用无参构造函数, 没有会报InstantiationException.
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;public class TestClassforName { public TestClassforName() {
System.out.println("Empty Paramter");
}
public TestClassforName(int i) {
System.out.println("Int Parameter " + i);
} /**
* @param args
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
// TODO Auto-generated method stub
Class c = Class.forName("TestClassforName");
Constructor cc = c.getConstructor(int.class);
cc.newInstance(3);
// .newInstance();
}}