最近,我工作中的一位开发人员开始在通常适用于枚举类型的地方使用类模式。他使用了类似下面这样的代码:
internal class Suit
{
public static readonly Suit Hearts = new Suit();
public static readonly Suit Diamonds = new Suit();
public static readonly Suit Spades = new Suit();
public static readonly Suit Clubs = new Suit();
public static readonly Suit Joker = new Suit();
private static Suit()
{
}
public static bool IsMatch(Suit lhs, Suit rhs)
{
return lhs.Equals(rhs) || (lhs.Equals(Joker) || rhs.Equals(Joker));
}
}
他的想法是使用一个看起来像枚举但实际上不是枚举的方式,这样可以使与枚举相关的方法(如上面的IsMatch)被包含在枚举本身内。
他称之为“枚举类”,但我之前从未见过。我想知道其优缺点以及如何获取更多信息?
谢谢。
编辑:他描述的另一个优点是能够为枚举添加特定的ToString()实现。