我以前从未使用过枚举状态,最近遇到了它,我发现要定义一个 FSM,其中您(程序员)知道状态,代码如下所示:
enum States {state0, state1, state2}; //an example of a state machine with 3 states as defined by the programmer.
但我希望能够定义一个 FSM,让用户决定有多少个状态以及有哪些状态,有没有办法做到这一点?
最佳答案
你不能在这里使用枚举,它有固定的大小,但我想你可以这样做:
public class StateMachine {
// it is up to you to define what a State and a Transition are
private Set<State> possibleStates;
private Set<Transition> transitions;
private State currentState;
// methods for adding and removing states
// ...
// methods for adding and removing transitions
// ...
}
关于java - 为具有 n 个状态的 FSM 定义状态。 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26898856/