Java 使用构造函数参数从类创建新实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4386870/
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
Creating new instance from Class with constructor parameter
提问by lhahne
I have situation where my Java class needs to create a ton of certain kind of objects. I would like to give the name of the class of the objects that are created as a parameter. In addition, I need to give the created class a parameter in its constructor. I have something like
我的 Java 类需要创建大量特定类型的对象。我想给出作为参数创建的对象的类的名称。此外,我需要在其构造函数中为创建的类提供一个参数。我有类似的东西
class Compressor {
Class ccos;
public Compressor(Class ccos) {
this.ccos = ccos;
}
public int getCompressedSize(byte[] array) {
OutputStream os = new ByteArrayOutputStream();
// the following doesn't work because ccos would need os as its constructor's parameter
OutputStream cos = (OutputStream) ccos.newInstance();
// ..
}
}
Do you have any ideas how I could remedy this?
你有什么想法我可以如何解决这个问题吗?
Edit:
编辑:
This is part of a research project where we need to evaluate the performance of multiple different compressors with multiple different inputs. Class ccos
is a compressed OutputStream
either from Java's standard library, Apache Compress Commons or lzma-java.
这是一个研究项目的一部分,我们需要评估具有多个不同输入的多个不同压缩机的性能。Class ccos
是OutputStream
来自 Java 标准库 Apache Compress Commons 或 lzma-java 的压缩文件。
Currently I have the following which appears to work fine. Other ideas are welcome.
目前我有以下似乎工作正常。欢迎其他想法。
OutputStream os = new ByteArrayOutputStream();
OutputStream compressedOut = (OutputStream) ccos.getConstructor(OutputStream.class).newInstance(os);
final InputStream sourceIn = new ByteArrayInputStream(array);
采纳答案by Bozho
You can use the Class.getConstructor(paramsTypes...)
method and call newInstance(..)
on the constructor. In your case:
您可以使用该Class.getConstructor(paramsTypes...)
方法并调用newInstance(..)
构造函数。在你的情况下:
Compressor.class.getConstructor(Class.class).newInstance(Some.class);
回答by NPE
You have to get to the relevant Constructor
object (e.g. via Class.getConstructors
or Class.getConstructor
) and then call constructor.newInstance
, giving it the arguments it requires.
您必须访问相关Constructor
对象(例如 viaClass.getConstructors
或Class.getConstructor
),然后调用constructor.newInstance
,为其提供所需的参数。
回答by WBAR
class Compresor<T> {
private Class<? extends T> clazz;
Compresor(final Class<? extends T> cls){
this.clazz = cls
}
}
回答by leroneb
An example you can use is as follows: lets say conn is a connection to the database.
您可以使用的示例如下:假设 conn 是到数据库的连接。
Class[] btarray = { conn.getClass() };
try {
if (classname != null) {
pmap = (Mapper) Class.forName(classname)
.getConstructor(btarray)
.newInstance(
new Object[] { conn }
);
}
} catch (Throwable x) {
x.printStackTrace(Log.out);
}
btarray allows you to pass in arguments to the constructor.
btarray 允许您将参数传递给构造函数。
回答by lisak
Using Spring ClassUtils and BeanUtils classes you can avoid dealing with those tedious exceptions that is Spring handling for you :
使用 Spring ClassUtils 和 BeanUtils 类,您可以避免处理 Spring 为您处理的那些乏味的异常:
Constructor<Car> constructor = ClassUtils.getConstructorIfAvailable(Wheels.class, Etc.class);
Car car = BeanUtils.instantiateClass(constructor, new Wheels(), new Etc());