从Java中的variables创build新的类

有没有办法从Java中的stringvariables创build一个新的类?

String className = "Class1"; //pseudocode follows Object xyz = new className(param1, param2); 

另外,如果可能的话,生成的对象必须是Objecttypes?

可能有更好的方法,但我希望能够从XML文件中检索值,然后创build以这些string命名的类。 每个类都实现相同的接口,并从相同的父类派生,所以我可以在该类中调用特定的方法。

这就是你想要做的:

 String className = "Class1"; Object xyz = Class.forName(className).newInstance(); 

请注意,newInstance方法不允许使用参数化构造函数。 (请参阅Class.newInstance文档 )

如果您确实需要使用参数化的构造函数,则需要执行以下操作:

 import java.lang.reflect.*; Param1Type param1; Param2Type param2; String className = "Class1"; Class cl = Class.forName(className); Constructor con = cl.getConstructor(Param1Type.class, Param2Type.class); Object xyz = con.newInstance(param1, param2); 

请参阅Constructor.newInstance文档

是的,你可以使用reflection,使用Class.forName(name),给你的类path加载一个类,抓取构造函数并调用它。 我会做你的例子。

考虑我有一个类:

 com.crossedstreams.thingy.Foo 

其中有签名的构造函数:

 Foo(String a, String b); 

我将基于这两个事实来实例化这个类,如下所示:

 // Load the Class. Must use fully qualified name here! Class clazz = Class.forName("com.crossedstreams.thingy.Foo"); // I need an array as follows to describe the signature Class[] parameters = new Class[] {String.class, String.class}; // Now I can get a reference to the right constructor Constructor constructor = clazz.getConstructor(parameters); // And I can use that Constructor to instantiate the class Object o = constructor.newInstance(new Object[] {"one", "two"}); // To prove it's really there... System.out.println(o); 

输出:

 com.crossedstreams.thingy.Foo@20cf2c80 

这里有很多资源详细介绍了这一点,你应该知道你正在引入一个编译器不能检查你的依赖项 – 如果你拼错了类名或者其他东西,它将在运行时失败。 另外,在这个过程中可能会抛出不同types的exception。 这是一个非常强大的技术。

这应该工作:

 import java.lang.reflect.*; FirstArgType arg1; SecondArgType arg2; Class cl = Class.forName("TheClassName"); Constructor con = cl.getConstructor(FirstArgType.class, SecondArgType.class); Object obj = con.newInstance(arg1, arg2); 

从那里你可以投射到一个已知的types。

在JDK7中,这对我来说干净些,而上面的答案使得事情比从新手的angular度来看要困难得多:(假设你声明'className'是作为方法parameter passing的stringvariables,或在使用此代码的方法中的较早版本):

 Class<?> panel = Class.forName( className ); JPanel newScreen = (JPanel) panel.newInstance(); 

从这里开始,您可以像使用它们一样使用dynamic命名的类中的属性/方法:

 JFrame frame = new JFrame(); // <<< Just so no-one gets lost here frame.getContentPane().removeAll(); frame.getContentPane().add( newScreen ); frame.validate(); frame.repaint(); 

上面其他答案中的例子导致错误,当我试图.add()新的'对象'types对象的框架。 这里显示的技术给了我一个可用的对象,只有上面的2行代码。

不完全确定这是为什么 – 我自己是一个Java新手。

另一个:

 import java.lang.reflect.Constructor; public class Test { public Test(String name) { this.name = name; } public String getName() { return name; } public String toString() { return this.name; } private String name; public static void main(String[] args) throws Exception { String className = "Test"; Class clazz = Class.forName(className); Constructor tc = clazz.getConstructor(String.class); Object t = tc.newInstance("John"); System.out.println(t); } } 

使用String对象获取实例的示例程序。

 public class GetStudentObjectFromString { public static void main (String[] args) throws java.lang.Exception { String className = "Student"; //Passed the Class Name in String Object. //A Object of Student will made by Invoking its Default Constructor. Object o = Class.forName(className).newInstance(); if(o instanceof Student) // Verify Your Instance that Is it Student Type or not? System.out.println("Hurrey You Got The Instance of " + className); } } class Student{ public Student(){ System.out.println("Constructor Invoked"); } } 

输出: –

  Constructor Invoked Hurrey You Got The Instance of Student