有人能告诉我有限状态机状态下的返回语句的用途是什么吗?例如,我有一个足球运动员状态的代码:
public class ChaseBall extends State<FieldPlayer> {
private static ChaseBall instance = new ChaseBall();
private ChaseBall() {
}
//this is a singleton
public static ChaseBall Instance() {
return instance;
}
@Override
public void Enter(FieldPlayer player) {
player.Steering().SeekOn();
}
}
@Override
public void Execute(FieldPlayer player) {
//if the ball is within kicking range the player changes state to KickBall.
if (player.BallWithinKickingRange() && player.isReadyForNextKick()) {
player.GetFSM().ChangeState(KickBall.Instance());
return;
}
//if the player is the closest player to the ball then he should keep
//chasing it
if (player.isClosestTeamMemberToBall()) {
player.Steering().SetTarget(player.Ball().Pos());
return;
}
//if the player is not closest to the ball anymore, he should return back
//to his home region and wait for another opportunity
player.GetFSM().ChangeState(ReturnToHomeRegion.Instance());
}
@Override
public void Exit(FieldPlayer player) {
player.Steering().SeekOff();
}
}
我想知道是否有人可以解释在Execute()方法的前两个if语句中返回关键字的用途是什么? 感谢
答案 0 :(得分:1)
在这种情况下,它主要是一系列else if子句的格式化替代。它在逻辑上等同于
if (<condition>) {
<code>
} else if (<condition>) {
<code>
} else {
<code>
}