UPD。你好,我知道下面的代码是如何工作的。我知道 cross 和 circle 指向 Cross() 和 Circle() 方法。但是我仍然对这部分代码感到困惑。你能给我解释一下吗?
public GameMoves()
{
cross = Cross();
circle = Circle();
}
所有代码
static void Main(string[] args)
{
GameMoves game = new GameMoves();
IEnumerator enumerator = game.Cross();
while (enumerator.MoveNext())
{
enumerator = (IEnumerator)enumerator.Current;
}
}
}
public class GameMoves
{
private IEnumerator cross;
private IEnumerator circle;
public GameMoves()
{
cross = Cross();
circle = Circle();
}
private int move = 0;
public IEnumerator Cross()
{
while (true)
{
Console.WriteLine("X, step {0}", move);
move++;
if (move > 9)
yield break;
yield return circle;
}
}
public IEnumerator Circle()
{
while (true)
{
Console.WriteLine("O, step {0}", move);
move++;
if (move > 9)
yield break;
yield return cross;
}
}
}