一个类只有一个有参数的构造函数,
请问可以用Class.forName()来创建这个类的对象吗?

解决方案 »

  1.   

    Class.forName()只是用来装载一个类吧
      

  2.   

    import java.lang.reflect.*;public class Test5 
    {
    public Test5(int i)
    {
    System.out.println(i);
    }

    public static void main(String[] args)
    {
    try {
    Constructor constructor = Test5.class.getDeclaredConstructor(new Class[]{int.class});
    Test5 test5 = (Test5)constructor.newInstance(new Object[]{new Integer(2)});
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  3.   

    Class.forName()?这样:public class Test5 
    {
    public Test5(int i)
    {
    System.out.println(i);
    }

    public static void main(String[] args)
    {
    try {
    Test5 test5 = (Test5)Class.forName("Test5").getDeclaredConstructor(new Class[]{int.class}).newInstance(new Object[]{new Integer(2)});
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }