这个问题与How to Queue and Call Actual Methods..有关。无论如何,我决定(毕竟)采用匿名班级的想法。问题是当我将我的匿名类添加到链接列表时,它实际上是立即调用 execute() ......它不应该是。稍后将调用 Execute()。无论如何,这就是我所拥有的:
private LinkedList<AgentAction> actions;
public boolean blockingSensor;
this.actions.add( new AgentAction(this) {
public void execute() {
//setRotationalVelocity(0);
kinematic.setWheelsVelocity(0,0);
this.agent.setBlockingSensors(false);
this.agent.printLCD("Turn, blocking = "+this.agent.blockingSensor);
}
public Object getValue() {
return null;
}
});
//this is essentially the main()
public void performBehavior()
{
//make sure to only call run() each tick, not every ms
if ( this.oldCounter < getCounter() )
{
if ( !isWorking() )
{
run();
}
this.oldCounter = getCounter();
this.actions.removeFirst().execute();
}
}
abstract class AgentAction
{
SimbadAgent agent;
public AgentAction(SimbadAgent a)
{
this.agent = a;
}
public abstract void execute();
public abstract Object getValue();
}
run() 是一个由子类实现的抽象方法。我只是不确定为什么它在添加时打印,而不是执行。我知道这意味着 performBehavior() 实际上被执行多次而不是每次执行一次,但事实并非如此。