我实现了你所说的两个函数,不知道是不是你想要的
[Flags]
public enum PCState
{
None = 1, Normal = 2, Special = 4, InQueue = 8
}
class Test
{
public PCState ChangeStateToSpecial(PCState curState, PCState special)
{
PCState ret = curState;
if ((ret & PCState.Normal) == PCState.Normal)
{
ret &= ~PCState.Normal;
}
return ret | special;
}
public PCState ChangeStateToNormal(PCState curState, PCState special)
{
PCState ret = curState;
if ((ret & special) == special)
{
ret &= ~special;
}
return ret | PCState.Normal;
}
}
调用:
static void Main(string[] args)
{
Test test = new Test();
PCState state=PCState.Normal|PCState.InQueue;
Console.WriteLine(state);
state = test.ChangeStateToSpecial(state, PCState.Special);
Console.WriteLine(state);
state = test.ChangeStateToNormal(state, PCState.Special);
Console.WriteLine(state);
}
输出:
Normal, InQueue
Special, InQueue
Normal, InQueue