[英]How to create an array of class types that can later be instantiated from the list?
I'll preface this question with the fact that I am not a professional Java programmer. 我将以我不是专业Java程序员的事实开头这个问题。 I come from a strong C embedded systems background, but was self-taught Java back in college.
我来自强大的C嵌入式系统背景,但是上大学时还是自学Java。
I am creating a simple program where there are many different data "producer" classes. 我正在创建一个简单的程序,其中有许多不同的数据“生产者”类。 Each class generates some kind of data output product.
每个类都会生成某种数据输出产品。
ArrayList< Class<? extends OtherClass> > list = new ArrayList< Class<? extends OtherClass> >();
//Register producers based on some criteria for the analysis being done
list.add(Producer1.class);
list.add(Producer2.class);
//Some time later, I want to create instances of those classes and link them to a 'report'
Report report = new Report();
report.addProducer( new list.get(0)() );
Of course, the above code does not work. 当然,上面的代码不起作用。 I am able to create an ArrayList of class types, but I am not able to instantiate them at a later time (at least I am not able to find the correct syntax to do so).
我可以创建一个类类型的ArrayList,但是以后不能实例化它们(至少我找不到这样做的正确语法)。
I also intended originally to do this with interfaces
, but the syntax for creating an array of classes does not seem to work like extends
in the above code sample. 我本来也打算使用
interfaces
来完成此interfaces
,但是用于创建类数组的语法似乎不像上述代码示例中的extends
那样起作用。 I tried the following, but it failed syntax checking: 我尝试了以下操作,但是语法检查失败:
ArrayList< Class<? implements OtherClass> > list = new ArrayList< Class<? implements OtherClass> >();
I searched around for a suitable answer, but not knowing the correct question/terminology to ask can make that difficult. 我四处寻找合适的答案,但不知道要问的正确问题/术语会令您感到困难。 I found this solution, but their intentions may have been different.
我找到了这个解决方案,但是他们的意图可能有所不同。
Of course, if this can be done in a simpler way then I am open to all suggestions. 当然,如果可以用更简单的方式完成此操作,那么我愿意接受所有建议。
Assuming all the classes in the list have no-arg constructor, you can instantiate them with 假设列表中的所有类都具有no-arg构造函数,则可以使用实例化它们
list.get(0).newInstance()
Also this works just fine for interfaces 而且这对于接口来说也很好
List<Class<? extends SomeInterface>>
The Producer interface: 生产者接口:
public interface Producer {
void produce();
}
First implementation: 第一次执行:
public class Producer1 implements Producer {
@Override
public void produce() {
System.out.println("Producer 1");
}
}
Second implementation: 第二实施:
public class Producer2 implements Producer {
@Override
public void produce() {
System.out.println("Producer 2");
}
}
Report class: 报告类别:
public class Report {
private final List<Producer> producers = new ArrayList<>();
public void addProducer(Producer producer) {
producers.add(producer);
}
public void produce() {
producers.forEach((p) -> p.produce());
}
}
Running following main method: 运行以下主要方法:
public static void main(String[] args) {
Report report = new Report();
List<Class<? extends Producer>> producers = new ArrayList<>();
producers.add(Producer1.class);
producers.add(Producer2.class);
producers.forEach((p) -> {
try {
report.addProducer(p.newInstance());
} catch (InstantiationException | IllegalAccessException ex) {
System.out.print("Ooops");
}
});
report.produce();
}
Will output to console: 将输出到控制台:
Producer 1
Producer 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.