【发布时间】:2021-12-29 08:47:55
【问题描述】:
我的问题是: 我创建了一个 Arraylist serviceList
service1 和 service2 都是来自 2 个类的 2 个独立对象,但它们具有相同的“执行”方法:
public Service1 service1;
public Service2 service2;
/*Then I add element to Arraylist:*/
serviceList.add(0, service1);
serviceList.add(1, service2);
/*then how can I run "excute" method of service1 and service2?
something like this:*/
for(Object service: serviceList){
service.execute();
}
我尝试使用 ArrayList
【问题讨论】:
-
您将它们声明为对象,对象不提供执行方法。所以要么你在你的循环中转换你的对象,要么你将它们声明为正确的类型
标签: java spring spring-boot spring-batch spring-batch-tasklet