本质上是一个二维枚举?

Essentially a 2 dimensional Enum?

不确定如何表达,但我有一个 class 称为 Lane 用于道路的不同车道。

车道有两部分 - 方向(左或右或其他的枚举),然后是数字,一个整数。所以车道看起来像这样:L1、L2、R1、R3 等。

每条泳道 class 应该只有一个泳道实例。 L1 不应该存在两次。

因此,我希望能够通过键入 Lane.L1 或 Lane.R4 等来分配对象的 Lane,就像您可以分配枚举一样。我目前必须使用一种方法来查找与我要引用的车道相对应的现有车道对象。

这是一种更好的方法吗?就像简单地输入 lane = Lane.L1,除了使 Lane class 具有私有构造函数并为每个可能的 Lane 手动创建 public Getter 并在静态构造函数中为车道 class?

这是车道的当前状态class:

public enum Direction { INCREASING = 1, DECREASING = 2, BOTH = 3, OTHER = 4, UNSPECIFIED = 5 }
public class Lane : IComparable
{
    public Direction Direction;
    public int Number = 1;
    public Lane(Direction direction, int number = 1)
    {
        Direction = direction;
        Number = number;
    }

    public override string ToString()
    {
        if (Direction == Direction.UNSPECIFIED)
        {
            return Direction.AsCharLR().ToString();
        }
        return Direction.AsCharLR() + Number.ToString();
    }

    public override bool Equals(object obj)
    {
        if (obj is Lane)
        {
            Lane l = obj as Lane;
            return Direction == l.Direction && Number == l.Number;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return (int)Direction * 100 + Number;
    }

    public static Lane Parse(char lane)
    {
        lane = char.ToUpper(lane);
        switch (lane)
        {
            case 'L':
            case 'I':
                return new Lane(Direction.INCREASING);
            case 'R':
            case 'D':
                return new Lane(Direction.DECREASING);
            case 'B':
                return new Lane(Direction.BOTH);
            case 'U':
                return new Lane(Direction.UNSPECIFIED);
            case 'O':
            default:
                return new Lane(Direction.OTHER);
        }
    }
    public static Lane Parse(string text)
    {
        Lane lane = Parse(text[0]);
        lane.Number = int.Parse(text.Substring(1));
        return lane;
    }

    public int CompareTo(object l)
    {
        return GetHashCode() - l.GetHashCode();
    }
}

好吧,你说过你不想要私有构造函数,但是如果你有 public 个静态属性呢,如下所示?:

public class Lane
{
    private Direction direction;
    private int number;

    public static Lane L1 = new Lane(Direction.INCREASING, 1);
    public static Lane L2 = new Lane(Direction.INCREASING, 2);
    //add more ...

    private Lane() { }

    private Lane(Direction d, int n)
    {
        this.direction = d;
        this.number = n;
    }

    public enum Direction
    {
        INCREASING = 1, DECREASING = 2, BOTH = 3, OTHER = 4, UNSPECIFIED = 5
    }
}

这样您可以键入 lane = Lane.L1,它总是 return 相同的实例。 这够了吗?

你不能用 enum 做你想做的事,因为它听起来不像你在 "number" 部分设置限制,而且你不能定义或使用运行时的新枚举。

这可能就是您所需要的:

public static class Lanes
{ 
    private static readonly Dictionary<string, Lane> LanesDictionary = 
        new Dictionary<string, Lane>();

    public static Lane GetLane(Direction direction, int number)
    {
        var key = direction.ToString() + number;
        if (LanesDictionary.ContainsKey(key))
        {
            return LanesDictionary[key];
        }
        var lane = new Lane(direction, number);
        LanesDictionary.Add(key, lane);
        return lane;
    }
}

现在每次引用 Lanes.GetLane(Direction.INCREASING, 4) 时,您总是会得到相同的 Lane。如果它不存在,它将被创建。

这为您提供了一种方便、可读的方式来引用给定的车道。如果出于某种原因你想要 shorthand 用于某些车道——尽管如前所述,你不想对它们进行硬编码(我也不会)——你可以这样做:

public static Lane L4 { get; } = GetLane(Direction.INCREASING, 4);