何时使用枚举,何时将其替换为具有静态成员的类? [英] When to use enums, and when to replace them with a class with static members?

查看:39
本文介绍了何时使用枚举,何时将其替换为具有静态成员的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近突然想到以下(示例)枚举...

It recently occured to me that the following (sample) enumeration...

enum Color
{
    Red,
    Green,
    Yellow,
    Blue
}

... 可以替换为看似更安全的类:

... could be replaced with a seemingly more type-safe class:

class Color
{
    private Color() { }

    public static readonly Color   Red      = new Color();
    public static readonly Color   Green    = new Color();
    public static readonly Color   Yellow   = new Color();
    public static readonly Color   Blue     = new Color();
}

对于类型安全",我的意思是如果 Color 是枚举,则以下语句将起作用,但如果 Color 是上述类,则不会:

With "type-safe", I mean that the following statement would work if Color was an enum, but not if Color were the above class:

var nonsenseColor = (Color)17;    // works if Color is an enum

两个问题:

1) 这种模式是否有一个被广泛接受的名称(将枚举替换为类型安全的类)?

Two questions:

1) Is there a widely accepted name to this pattern (replacing an enum with a type-safe class)?

2) 在什么情况下应该使用枚举,什么时候使用类更合适?

2) In which cases should one use enums, and when would a class be more appropriate?

推荐答案

枚举非常适合轻量级状态信息.例如,您的颜色枚举(不包括蓝色)非常适合查询交通灯的状态.真正的颜色以及颜色的整个概念及其所有包袱(alpha、颜色空间等)并不重要,只是光线处于哪个状态.另外,稍微改变你的枚举来表示交通灯的状态:

Enums are great for lightweight state information. For example, your color enum (excluding blue) would be good for querying the state of a traffic light. The true color along with the whole concept of color and all its baggage (alpha, color space, etc) don't matter, just which state is the light in. Also, changing your enum a little to represent the state of the traffic light:

[Flags()]
public enum LightColors
{
    unknown = 0,
    red = 1,
    yellow = 2,
    green = 4,
    green_arrow = 8
}

当前灯光状态可以设置为:

The current light state could be set as:

LightColors c = LightColors.red | LightColors.green_arrow;

并查询为:

if ((c & LightColors.red) == LightColors.red)
{
    //Don't drive
}
else if ((c & LightColors.green_arrow) == LightColors.green_arrow)
{
    //Turn
}

静态类颜色成员无需额外功能即可支持这种多状态.

Static class color members would be able to support this multiple state without extra functionality.

然而,静态类成员对于常用对象来说非常棒.System.Drawing.Color 成员就是很好的例子,因为它们代表具有晦涩构造函数的已知颜色(除非您知道十六进制颜色).如果它们被实现为枚举,那么每次你想将值用作颜色时都必须这样做:

However, static class members are wonderful for commonly used objects. The System.Drawing.Color members are great examples as they represent a known-name colors that have obscure constructors (unless you know your hex colors). If they were implemented as enums you would have to do something like this every time you wanted to use the value as a color:

colors c = colors.red;
switch (c)
{
    case colors.red:
        return System.Drawing.Color.FromArgb(255, 0, 0);
        break;
    case colors.green:
        return System.Drawing.Color.FromArgb(0,255,0);
        break;
}

因此,如果您有一个枚举并发现您不断地执行 switch/case/if/else/whatever 来派生一个对象,您可能想要使用静态类成员.如果您只是查询某物的状态,我会坚持使用枚举.此外,如果您必须以不安全的方式传递数据,枚举可能会比您的对象的序列化版本更好地生存.

So if you've got an enum and find that your constantly doing a switch/case/if/else/whatever to derive an object, you might want to use static class members. If you're only querying the state of something, I'd stick with enums. Also, if you have to pass data around in an unsafe fashion enums will probably survive better than a serialized version of your object.

@stakx,我认为您在回复@Anton 的帖子时也偶然发现了一些重要的东西,那就是复杂性,或者更重要的是,它对谁复杂?

@stakx, I think you stumbled on something important, too in response to @Anton's post and that is complexity or more importantly, who is it complex for?

从消费者的角度来看,我更喜欢 System.Drawing.Color 静态类成员,而不是必须编写所有这些.然而,从制作人的角度来看,必须写下所有这些内容会很痛苦.因此,如果其他人要使用您的代码,您可能会通过使用静态类成员为他们省去很多麻烦,即使编写/测试/调试可能需要 10 倍的时间.但是,如果只是您自己,您可能会发现只使用枚举并根据需要进行转换/转换会更容易.

From a consumer's standpoint, I would immensely prefer System.Drawing.Color static class members over having to write all of that. From a producer's standpoint, however, it would be a pain to have to write all of that. So if other people are going to be using your code you might be saving them a lot of trouble by using static class members even though it might take you 10 times as long to write/test/debug. However, if its just you you might find it easier to just use enums and cast/convert as needed.

这篇关于何时使用枚举,何时将其替换为具有静态成员的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆