我正在尝试将一些c#代码转换为java,我遇到了一些困难。
我有以下代码,我想知道如何用Java实现CurrentState行。
public class StateMachine
{
**public static State CurrentState { get; set; }**
public enum State
{
Init,
Data,
Text,
Close,
Invalid
}
}
这是我解决问题的微薄尝试,但这不太对。
static State currentState;
public static void setCurrentState(State currentState)
{
this.currentState = currentState;
}
public static State getCurrentState()
{
return currentState;
}
现在已经回答了问题,这是正确的工作代码,以防有人想看到它。
public static State currentState;
public static State getCurrentState()
{
return currentState;
}
public static void setCurrentState(State newState)
{
currentState = newState;
}
使用这些getCurrentState和setCurrentState方法,我现在可以在原始目标的case语句中轻松实现它。
答案 0 :(得分:1)
要回答基本问题,您需要为get和set声明一个名为state
的支持字段。
public class StateMachine {
// This is the missing part
private static State state;
public static State getState() {
return state;
}
public static void setState(State newState) {
state = newState;
}
}