简体   繁体   English

如何在Java运行时通过使用类名获取类对象

[英]How to get class Object by using class Name at runtime in java

I am working on java,In run time I got class name but I want to create object for that class how can I do this any one help me. 我正在使用Java,在运行时我获得了类名,但是我想为该类创建对象,我该如何做到这一点呢? I tried below way please check my code below. 我尝试了以下方式,请在下面检查我的代码。

String emp = "Emp" // this is the classname i got in runtime(I will get dynamical this name)
Object obj = class.forName().newInstance();

up to this it is working fine ,but I got java.lang.Object in above code I don't want that object I want to convert that object into my className(Emp)object. 到此为止,它工作正常,但是我在上面的代码中得到了java.lang.Object,我不想要该对象,而是想要将该对象转换为我的className(Emp)对象。 like 喜欢

Emp e =(Emp)obj;

Note: I am not sure every time I will get Emp classname at runtime total I have 100 Classes in myproject so any classname I will get here(like Emp,Dept,Student.....). 注意:我不确定每次在运行时总共获得Emp类名时,我的项目中都会有100个类,所以我会在这里得到任何类名(例如Emp,Dept,Student .....)。

Maybe I don't understand your question, but if you are looking to create a class instance only using your class name, you can try this method: 也许我不明白您的问题,但是如果您希望仅使用类名来创建类实例,则可以尝试以下方法:

public static MyClass getFromString(String myClassName) {
     Object result = null;
     MyClass final_result = null;

     try{
         result = Class.forName(myClassName).getConstructor().newInstance();
         final_result= (MyClass)result;
     }catch(ClassNotFoundException e){} //you must specify concrete Exception here

     return final_result; 
    }

Hope it helps. 希望能帮助到你。

It's not clear what you are trying to achieve. 目前尚不清楚您要实现的目标。 If you are confident that the class you loaded dynamically is Emp (or a subclass of it) it is sufficient to do the type cast like you did in your question. 如果您确信动态加载的类是Emp (或其子类),则可以像在问题中所做的那样进行类型转换。 If you have doubts about the dynamically loaded type being compatible with Emp , because of the one hundred other classes you have, then it's not clear what you expect from us. 如果由于拥有100个其他类而对动态加载的类型与Emp兼容感到怀疑,则不清楚您对我们的期望。

What you can do is to separate the type checking from the instantiation, eg 您可以做的是将类型检查与实例化分开,例如

Class<? extends Emp> type = Class.forName(className).asSubclass(Emp.class);

Emp instance = type.newInstance();

This checks whether the type is compatible with Emp in the first line, thus the second line is guaranteed to return an instance of Emp . 这将检查第一行中的类型是否与Emp兼容,从而确保第二行返回Emp的实例。

However, the way, an incompatible type is reported does not change. 但是,报告不兼容类型的方式不会改变。 It will be done by throwing a ClassCastException . 这将通过抛出ClassCastException来完成。

If that's not what you were asking for, you should reword your question. 如果这不是您要的,则应重新输入您的问题。

Object object=Class.forName(className).getConstructor().newInstance();
  // Complete class name, including Package
  String name = "com.foo.Emp";

  Object object = Class.forName(name).getConstructor().newInstance();
  if (object instanceof Emp) {
     // Specific logic for Emp objects
  } else if (object instanceof Dept) {
     // Specific logic for Dept objects
  }

Hope it helps you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM