【发布时间】:2020-11-25 05:15:09
【问题描述】:
我有一个名为Intent 的Java 类。在名为intents 的目录中,我定义了Intent 的几个子类。现在,在我的 runner 类中,我想将每个子类实例化为一个数组列表,如下所示:
public static String parseTranscript(String transcript) {
ArrayList<Intent> intents = new ArrayList<Intent>();
File[] intentFiles = new File("./intents").listFiles();
for (File fileName : intentFiles) {
//for each of the intents defined in "intents/",
//create a new class and add to the array list.
//intents.add(new fileName.ObjectName());
}
}
所以,如果我理解正确,我需要打开目录,获取所有文件名,然后从该文件名创建一个对象。最好的方法是什么?
文件结构:
- Intent.java
- Main.java
- intents/
- HelloIntent.java
- GameIntent.java
.
.
.
目标是做到这一点不必在运行器中手动定义每个子类。
【问题讨论】:
-
这取决于您运行的环境。最常见的情况是作为基准,我建议查看SPI。
标签: java file class object dynamic