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

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

问题描述

我最近发现以下(示例)枚举...

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天全站免登陆