将用 Java 编写的枚举翻译成 C#

Translate enum written in Java to C#

我正在翻译 Java 中写的火星探测器问题的解决方案。我不确定如何处理找到的方向枚举 class here.

我不认为我可以在 C# 中做到这一点,因此我想问问是否有人可以建议如何做到这一点。

我正在考虑为从该接口继承的每个方向创建一个 IDirection 接口和一个 class。

尽管 java 似乎具有 C# 所没有的这一特性,但事实是(一如既往)C# 是一种更现代的语言,它需要更少的代码来生成相同的结果。

您的 72 行 java 代码可以翻译成这些 35 行 C#:

public class Direction
{
    public static readonly Direction N = new Direction(0, 1);
    public static readonly Direction S = new Direction(0, -1);
    public static readonly Direction E = new Direction(1, 0);
    public static readonly Direction W = new Direction(-1, 0);

    private Direction(int stepSizeX, int stepSizeY)
    {
        this.StepSizeForXAxis = stepSizeX;
        this.StepSizeForYAxis = stepSizeY;
    }

    static Direction()
    {
        N.Left = W;
        N.Right = E;
        S.Left = E;
        S.Right = W;
        E.Left = N;
        E.Right = S;
        W.Left = S;
        W.Right = N;
    }

    public Direction Left { get; private set; }
    public Direction Right { get; private set; }
    public int StepSizeForXAxis { get; private set; }
    public int StepSizeForYAxis { get; private set; }
}

这导致 class 只能由自身实例化(因为私有构造函数),并且具有可以像使用 C# enum 一样使用的成员,具有附加属性的优势:

var south = Direction.S;
var east = south.Left;
Console.WriteLine(east == south); // True
Console.WriteLine(south.StepSizeForXAxis); //0
Console.WriteLine(south.StepSizeForYAxis); //-1

java 很难再与 C# 相比,更不用说比它有任何优势了,至少在语言层面是这样。

在 C# 中,枚举是对一组有限原始类型的简单包装类型,遗憾的是扩展方法是扩展它们的唯一方法。
在 Java 中,它们是 类,您可以使用它们自己的方法对其进行扩展。

您需要做的就是效仿该行为。一种可能的方法是:

public sealed class Direction {

    public static readonly Direction N = new Direction(0, 1);
    public static readonly Direction S = new Direction(0, -1);
    public static readonly Direction E = new Direction(1, 0);
    public static readonly Direction W = new Direction(-1, 0);

    static Direction() {
        N.Left = W;
        N.Right = E;
        S.Left = E;
        S.Right = W;
        E.Left = N;
        E.Right = S;
        W.Left = S;
        W.Right = N;
    }

    private Direction(int stepSizeOnXAxis, int stepSizeOnYAxis)
    {
        StepSizeForXAxis = stepSizeOnXAxis;
        StepSizeForYAxis = stepSizeOnYAxis;
    }

    public Direction Right { get; private set; }
    public Direction Left { get; private set; }

    public int StepSizeForXAxis { get; }
    public int StepSizeForYAxis { get; }
}

您可以为此目的使用 struct,其中一组选定的全局单例代表标准方向。我建议使用 struct,因为您的 Direction 对象具有值语义:

public struct Direction : IEquatable<Direction>
{
    short xstep;
    short ystep;

    public static Direction N { get { return new Direction(0, 1); } }
    public static Direction E { get { return new Direction(1, 0); } }
    public static Direction S { get { return new Direction(0, -1); } }
    public static Direction W { get { return new Direction(-1, 0); } }

    public static IEnumerable<Direction> Directions
    {
        get
        {
            yield return N;
            yield return E;
            yield return S;
            yield return W;
        }
    }

    Direction(int x, int y)
    {
        this.xstep = checked((short)x);
        this.ystep = checked((short)y);
    }

    public int XStep { get { return xstep; } }

    public int YStep { get { return ystep; } }

    public Direction Left { get { return new Direction(-YStep, XStep); } }

    public Direction Right { get { return new Direction(YStep, -XStep); } }

    public override bool Equals(object obj)
    {
        if (obj is Direction)
        {
            var other = (Direction)obj;
            return xstep == other.XStep && ystep == other.YStep;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return (XStep.GetHashCode() | (YStep << 16).GetHashCode());
    }

    #region IEquatable<Direction> Members

    public bool Equals(Direction other)
    {
        return this.xstep == other.xstep && this.ystep == other.ystep;
    }

    #endregion

    public static Direction operator -(Direction direction)
    {
        return new Direction(-direction.XStep, -direction.YStep);
    }

    public static bool operator ==(Direction first, Direction second)
    {
        return first.Equals(second);
    }

    public static bool operator !=(Direction first, Direction second)
    {
        return !(first == second);
    }

    public override string ToString()
    {
        if (this == Direction.N)
            return "N";
        if (this == Direction.E)
            return "E";
        if (this == Direction.S)
            return "S";
        if (this == Direction.W)
            return "W";
        return string.Format("({0},{1}}", XStep.ToString(NumberFormatInfo.InvariantInfo), YStep.ToString(NumberFormatInfo.InvariantInfo));
    }
}

public static bool operator ==(Direction first, Direction second) 允许使用 == 运算符简单地比较方向。这也需要覆盖 EqualsGetHashCode().