何时使用枚举,当他们与静态成员的类取代? [英] When to use enums, and when to replace them with a class with static members?

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

问题描述

该公司最近发生,我认为以下(样品)枚举...

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();
}

使用类型安全,我的意思是,如果颜色是一个枚举下面的语句会的工作,但如果颜色是上面的类:

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?

推荐答案

枚举是伟大的轻量级的状态信息。例如,你的颜色枚举(不包括蓝)将是很好的查询交通灯的状态。真正的颜色与颜色和它的所有行李(α,色彩空间等)没有关系的整体概念一起,就这状态是在光,同时,改变你的枚举一点点重新present状态红绿灯的:

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 成员都是很好的例子,因为他们重新present一个已知的名称颜色有模糊的构造函数(除非你知道你的十六进制的颜色)。如果他们为枚举实现你要每个你想使用的值的时间做这样的事情作为颜色:

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 /不管派生一个对象,你可能想使用静态类成员。如果你只查询的一些状况,我会用枚举坚持。另外,如果你要在一个不安全的方式枚举各地传递数据可能会生存下去比你的对象的序列化版本更好。

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,我想你无意中发现了一些重要的东西,太多回应@安东的帖子,这是复杂或更重要的是,是谁对复杂?

@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?

从消费者的角度来看,我将大大p $ PFER的System.Drawing.Color类的静态成员美元,不得不写了这一切。从生产者的角度来看,不过,这将是一个痛苦写了这一切。因此,如果其他人将要使用code你可能会通过使用静态类成员,即使它可能需要您只要编写/测试/调试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天全站免登陆