我正在翻译用Java编写的Mars Rover问题的解决方案。我不确定如何处理找到here的方向枚举类。
我不认为我可以在C#中这样做,因此我想问一下是否有人可以建议如何做到这一点。
我正在考虑为从该接口继承的每个Direction创建一个IDirection接口和一个类。
答案 0 :(得分:2)
尽管java似乎具有C#所没有的这个特性,但事实上(一如既往)C#是一种更现代的语言,它需要更少的代码来产生相同的结果。
您的 72 行代码可以转换为C#的 35 行:
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; }
}
这导致一个类只能由它自己实例化(因为私有构造函数),并且有一些成员可以像使用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#相提并论了,更不用说声称它具有任何优势,至少在语言层面是这样。
答案 1 :(得分:2)
在C#中,enums是一组基本类型的简单包装器类型,遗憾的是,扩展方法是扩展它们的唯一方法。
在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; }
}
答案 2 :(得分:2)
您可以使用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)
只需使用==
运算符即可比较路线。这还需要覆盖Equals
和GetHashCode()
。