我試圖以編程方式初始化類並將其添加到列表中。我不知道如何輸入workflowClass
以便它extends BaseWorkflow
因此列表將接受它。目前它說我預計? extends BaseWorkflow
時不能添加Object
。如何以編程方式強制類型轉換初始化類
private static List<? extends BaseWorkflow> workflowList = new ArrayList<BaseWorkflow>();
/**
* Initialize the production line
*/
public static void init() {
//load active workflows into memory
WorkflowService workflowService = (WorkflowService) ApplicationContextProvider.getApplicationContext().getBean("workflowService");
for (Workflow workflow: workflowService.findActive()) {
try {
Class<?> clazz = Class.forName(workflow.getClassName());
Object workflowClass = clazz.newInstance();
workflowList.add(workflowClass);
這可能幫助:http://stackoverflow.com/questions/356583 –