java 动态创建一个对象,其中类名作为字符串给出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12812773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:19:27  来源:igfitidea点击:

Creating an object dynamically where the class name is given as a string

java

提问by Praveen

I'm trying to create an object of a class dynamically by sending the class name as a string. I have searched in all the java forums but i couldn't get the answer which i wanted. Here is my requirement, i have a class with name Agent,

我试图通过将类名作为字符串发送来动态创建类的对象。我已经在所有的 Java 论坛中进行了搜索,但没有得到我想要的答案。这是我的要求,我有一个名为 Agent 的类,

package somePack;

public class Agent{
  private String Id;
  Private String Name;
  public String getId() {
    return this.id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getName() {
    return this.name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

package somePack;

public class Employee{
  private String Id;
  Private String Name;
  public String getId() {
    return this.id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getName() {
    return this.name;
  }
  public void setName(String name) {
    this.name = name;
  }
}
public class create{
   public static void main(String[] args){
      newCreate("Employee");
      newCreate("Agent");
   }
   public static void newCreate(String name){
      String path="somePack."+name;
      Class cls=Class.forName(path);
      System.out.println("Class Name " + cls.getName());
  }
}

Now my question is, cls.getName() gives me the class name, but now i want to create the object for that class i.e., for Employee class and Agent class respectively, but how can i create the object for them? The string that is passed may be something else from the other method, how can i create an object for such kind of things.

现在我的问题是, cls.getName() 给了我类名,但现在我想为该类创建对象,即分别为 Employee 类和 Agent 类创建对象,但我如何为它们创建对象?传递的字符串可能是其他方法的其他内容,我如何为此类事物创建对象。

Can anyone help me....

谁能帮我....

Thank you in advance,

先感谢您,

回答by Jon Skeet

but now i want to create the object for that class

但现在我想为那个类创建对象

Then use the Class.newInstance()method if you never need to handle constructors with arguments, or use Class.getConstructor(...)or Class.getConstructors()otherwise. (Call Constructor.newInstance()to invoke the constructor.)

Class.newInstance()如果您永远不需要处理带参数的构造函数,则使用该方法,或者使用Class.getConstructor(...)Class.getConstructors()以其他方式。(Constructor.newInstance()调用构造函数的调用。)

回答by Matthew Farwell

Use Class#newInstance()

使用Class#newInstance()

Object o = Class.forName(path).newInstance();

You can then cast this to Agent.

然后您可以将其转换为 Agent。

Agent agent = (Agent) Class.forName(path).newInstance();

回答by MadProgrammer

On the grounds that the class has a default constructor (ie no parameters), you can simply call Class#newInstance.

由于该类具有默认构造函数(即没有参数),您可以简单地调用Class#newInstance.

If it has parameters, it becomes a little more complicated.

如果它有参数,它会变得更复杂一些。

You'd have to get a reference to the appropriate Constructorvia Class#getConstructor, passing in the expected parameter class types.

您必须获得对适当Constructorvia的引用Class#getConstructor,并传入预期的参数类类型。

Once you have that, you can call Constructor#newInstancepassing it the appropriate objects

一旦你有了它,你就可以调用Constructor#newInstance它传递适当的对象