假设我有这样的方法
 public void readAndInitialize(StringReader reader, Class className)
  {
    // Here, I want to to something like this - 
   // obj = new (object of the specified className)
      obj.readAndInitialize(reader);
  }
如何在 Java 中做到这一点?
假设我有这样的方法
 public void readAndInitialize(StringReader reader, Class className)
  {
    // Here, I want to to something like this - 
   // obj = new (object of the specified className)
      obj.readAndInitialize(reader);
  }
如何在 Java 中做到这一点?
虽然每个人都很快指出了Class.forName("com.myorg.MyClass");相关newInstance()方法,但重要的是要记住它只会调用不带参数的默认构造函数。
如果发现需要调用该类的特定构造函数,则需要使用反射找到正确的构造函数,然后调用它。
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import static java.lang.System.out;
public class ConstructorSift {
    public static void main(String... args) {
    try {
        Class<?> cArg = Class.forName(args[1]);
        Class<?> c = Class.forName(args[0]);
        Constructor[] allConstructors = c.getDeclaredConstructors();
        for (Constructor ctor : allConstructors) {
        Class<?>[] pType  = ctor.getParameterTypes();
        for (int i = 0; i < pType.length; i++) {
            if (pType[i].equals(cArg)) {
            out.format("%s%n", ctor.toGenericString());
            Type[] gpType = ctor.getGenericParameterTypes();
            for (int j = 0; j < gpType.length; j++) {
                char ch = (pType[j].equals(cArg) ? '*' : ' ');
                out.format("%7c%s[%d]: %s%n", ch,
                       "GenericParameterType", j, gpType[j]);
            }
            break;
            }
        }
        }
        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
    }
}
列出了所有的构造函数,这里有一个教程。
找到所需的构造函数后,可以像这样调用它
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import static java.lang.System.out;
class EmailAliases {
    private Set<String> aliases;
    private EmailAliases(HashMap<String, String> h) {
    aliases = h.keySet();
    }
    public void printKeys() {
    out.format("Mail keys:%n");
    for (String k : aliases)
        out.format("  %s%n", k);
    }
}
public class RestoreAliases {
    private static Map<String, String> defaultAliases = new HashMap<String, String>();
    static {
    defaultAliases.put("Duke", "duke@i-love-java");
    defaultAliases.put("Fang", "fang@evil-jealous-twin");
    }
    public static void main(String... args) {
    try {
        Constructor ctor = EmailAliases.class.getDeclaredConstructor(HashMap.class);
        ctor.setAccessible(true);
        EmailAliases email = (EmailAliases)ctor.newInstance(defaultAliases);
        email.printKeys();
        // production code should handle these exceptions more gracefully
    } catch (InstantiationException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    } catch (InvocationTargetException x) {
        x.printStackTrace();
    } catch (NoSuchMethodException x) {
        x.printStackTrace();
    }
    }
}
同样,来自关于反射的教程。
请注意,在您的示例中,您将 className 作为Class对象传递,因此您可以这样做:
className.newInstance();
但我怀疑您正在寻找一种将类名作为 a 传递的方法String,在这种情况下,过程是(这里className是 a String):
Class class = Class.forName(className());
Object obj = class.newInstance();
请注意,您还需要处理Exceptions两个调用引发的问题。
这是最简单的情况,不包括,例如特定的构造函数。
请参阅Class的文档。
public void readAndInitialize(StringReader reader, String className)  //className changed to String
  {
    // Here, I want to to something like this - 
   // obj = new (object of the specified className)
      //try this:
      Class myclass = Class.forName(className);
      Object obj = myclass.newInstance();
      obj.readAndInitialize(reader);
  }
如果我正确理解了这个问题。应该像这样创建给定类的对象:
if (yourClass != null) {
    Object yourObject = null;
    try {
        yourObject = yourClass.newInstance();
    } catch (InstantiationException e) {
        LOGGER.error("", e);            
    } catch (IllegalAccessException e) {
        LOGGER.error("", e);
    }
}
您可以使用 Class.newInstance()(仅适用于零参数构造函数)或 Constructor.newInstance() 来做您想做的事。有关工作示例,请参见此处。
取决于您是否已经获得了一个对象或只是一个带有类名的字符串,我会执行以下操作: