如:Vector test = new Vector();现在我想问一下,如果“Vector”是个对象变量(如:var),不确定,请问这个该怎么写??谢谢

解决方案 »

  1.   

    Object test;test = new Vector();
      

  2.   

    运用反射:
    假设对象变量为"car",
    import java.lang.refelct.*:
    public void Test(Class var){Constructor a= var.getConstructor();
    a.newInstance()//生成一个var对象,相当于new.
      

  3.   


    Object o=....;
    test.add(o);
      

  4.   

    hehe,看错了楼主的意思。要用反射机制了
      

  5.   

    给你个小测试程序吧!
    import java.lang.reflect.*;
    class A{
    public A(){
    System.out.println("a");
    }
    }
    public class TestA {
    public static Object test(Class var)throws Exception{
      
       Constructor a= var.getConstructor();
         return  a.newInstance();
    }
    public static void main(String [] args)throws Exception {
     
            A a = (A)test(A.class); 
    }
    }