您可以使用工厂模式.
创建一个接口并在A,B,C和D的基本方法内定义.
然后所有A,B,C和D类都必须实现该接口.
public interface BaseInterface {
// your methods
}
然后创建一个Factory类,在该类中传递一个标识符,它将使您的阅读器正确启动
package a;
public final class Factory {
// Not instantiable
private Factory() {
throw new AssertionError("Not instantiable");
}
public static CSVReader getReader(String reader) {
if ("A".equals(reader)) {
return new CSVReader();
} else if ("B".equals(reader)) {
return new CSVReader();
}
// TODO create all your readers
}
}
现在,您可以通过您的工厂类呼叫读者,如下所示:
ValueProcessorProvider provider = new ValueProcessorProvider();
CSVEntryParser entryParser = new AnnotationEntryParser(A.class, provider);
CSVReader newExternalFileCSVReader =
Factory("your reader type");
List m_NewExternalFileData = newExternalFileCSVReader.readAll();
由于你没有发布A,B,C和D类,你必须自定义该代码,但按照这种方式,我认为你可以完成你想要的.