如何在演员添加到舞台时添加回调?
问题描述:
我有一个在某个特定时间添加到舞台上的演员。加入舞台后需要计算价值。 有没有办法在添加到阶段之后向Actor添加回调?如何在演员添加到舞台时添加回调?
示例代码
public class SlotReel extends Table{
public SlotReel() {
}
public void compute(){
//call after SlootReel is added to stage
}
}
例阶段添加代码
stage.addActor(slotReel);// I wish to trigger the compute method in SlotReel after here.
答
例
public class Solution {
// Create interface
interface Computable {
void compute();
}
// SlotReel implement Computable interface
static public class SlotReel implements Computable {
String name;
public SlotReel(String name) {
this.name = name;
}
// Implement compute method
@Override
public void compute() {
// call after SlootReel is added to stage
// Just an example
System.out.println("Hello " + name);
}
}
static public class Stage {
List<Computable> list = new ArrayList<>();
public void addActor(Computable slot) {
list.add(slot);
// call compute method
slot.compute();
}
}
public static void main(String[] args) {
Stage stage = new Stage();
stage.addActor(new SlotReel("A"));
stage.addActor(new SlotReel("B"));
stage.addActor(new SlotReel("C"));
stage.addActor(new SlotReel("D"));
}
}