在类构造函数中初始化枚举的正确方法

发布于 2024-12-08 03:37:44 字数 664 浏览 2 评论 0原文

我可能以错误的方式看待枚举,但想确保我在如何使用它们方面拥有正确的理论。

假设我们有一个名为 Colour 的枚举。

enum Colour { Red, Green, Blue };

红、绿、蓝用 0-255 的值表示。 我正在尝试在类形状内初始化此枚举,但我不太确定如何进行。

public class Shape
{

    Colour colour;

    public Shape(Colour c)
    {
         //Some attempts at initialization.


         //Treating It like an object
         this.colour = 
            c{
                255,255,255
            };

         //Again
         this.colour.Red = c.Red
         this.colour.Blue = c.Blue
         this.colour.Green = c.Green

         Colour.red = c.red?


         }
    }
}

我对枚举的看法可能还很遥远。有人能给我一些指点吗?

I could be looking at Enums in the wrong way but want to make sure I have the right theory in how to use them.

Say we have an enum called Colour.

enum Colour { Red, Green, Blue };

Red Green and Blue are represented by there 0-255 values.
I'm trying to initialize this enum inside a class shape and I'm not really sure how to go about it.

public class Shape
{

    Colour colour;

    public Shape(Colour c)
    {
         //Some attempts at initialization.


         //Treating It like an object
         this.colour = 
            c{
                255,255,255
            };

         //Again
         this.colour.Red = c.Red
         this.colour.Blue = c.Blue
         this.colour.Green = c.Green

         Colour.red = c.red?


         }
    }
}

I'm probably way off in terms of how I'm thinking about enums. Can anyone give me some pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

千と千尋 2024-12-15 03:37:44

在这种情况下,您可能希望 Color 是 struct 而不是枚举。在 C# 中,枚举是单值构造,但有三个值(红色、绿色和蓝色)。我可能会这样做:

public struct Colour 
{
    private byte red;
    private byte green;
    private byte blue;

    public Colour(byte r, byte g, byte b) 
    {
        this.red = r;
        this.green = g;
        this.blue = b;
    }
}

public class Shape
{
    public Colour Colour { get; private set; }

    public Shape(Colour c)
    {
        this.Colour = c;
    }
}

然后当您创建形状对象时:

var shape = new Shape(new Colour(203, 211, 48));

编辑:正如克里斯在评论中指出的那样,您可以简单地使用System.Drawing.Color 框架提供的结构体。上面的例子可以简化为:

using System.Drawing;

public class Shape
{
    public Color Colour { get; private set; }

    public Shape(Color c)
    {
        this.Colour = c;
    }
}

var shape = new Shape(Color.FromArgb(203, 211, 48));

In this case, you might want Colour to be struct instead of an enum. In C#, enums are single-valued constructs, but you have three values (red, green, and blue). Here's what I might do instead:

public struct Colour 
{
    private byte red;
    private byte green;
    private byte blue;

    public Colour(byte r, byte g, byte b) 
    {
        this.red = r;
        this.green = g;
        this.blue = b;
    }
}

public class Shape
{
    public Colour Colour { get; private set; }

    public Shape(Colour c)
    {
        this.Colour = c;
    }
}

And then when you're creating your shape objects:

var shape = new Shape(new Colour(203, 211, 48));

EDIT: As Chris pointed out in the comments, you could simply use the System.Drawing.Color struct provided by the framework. The example above would be simplified to:

using System.Drawing;

public class Shape
{
    public Color Colour { get; private set; }

    public Shape(Color c)
    {
        this.Colour = c;
    }
}

var shape = new Shape(Color.FromArgb(203, 211, 48));
源来凯始玺欢你 2024-12-15 03:37:44

枚举非常类似于关系数据库中的类型(或关联)表。它是一组选项列表,因此您可以限制一个值。如果您不太熟悉关系数据库,则可以将枚举视为“选择列表”。枚举为我们提供了一个小的、有限的变量选择列表,而基础类型(int、byte 等)则要大得多。

完成您正在尝试的操作的正常方法是测试枚举值,然后设置您的对象:

switch(c)
{
   case Colour.Red:
        //Set up red shape here
        break;
   //etc ...
}

An enum is very much like a Type (or Association) table in a relational database. It is a set list of options so you can constrain a value. Think of an enum as a "pick list" perhaps, if you are not as well versed with relational databases. Enums give us a small, finite list of choices for a variable whereas the underlying type (int, byte, etc) is much larger.

The normal method to accomplish what you are trying is to test the enum value and then set up your object:

switch(c)
{
   case Colour.Red:
        //Set up red shape here
        break;
   //etc ...
}
凶凌 2024-12-15 03:37:44
this.colour = Colour.Red; // or Colour.Green or whatever

this.colour 属于 Colour 类型,并且只能采用三个值之一。将 Colour 视为类似于 Integer 的类型,但它只能采用三个值之一,而 Integer 可以采用 中的任何一个值>Integer.MIN_VALUE..Integer.MAX_VALUE 范围。

如果您尝试从中构建 RGB 颜色,那么这不是正确的方法。在这种情况下,您正在寻找一个名为 Colour 的类,它的红色、绿色和蓝色 分量具有不同的值。

this.colour = Colour.Red; // or Colour.Green or whatever

this.colour is of type Colour and can take only one of the three values. Think of Colour as a type like Integer but it can take one of only three values while Integer can take any one of the values in Integer.MIN_VALUE..Integer.MAX_VALUE range.

If you are trying to construct an RGB colour out of it then this is not the right approach. In that case you are looking for a class named Colour which has different values for its red, green and blue components.

世俗缘 2024-12-15 03:37:44

您的枚举 Colour 没有您定义的每个枚举值的属性。 Red、Green 和 Blue 值是您的枚举可能的值。

您想要执行类似 this.colour = Colour.Red 的操作,将 colour 变量设置为红色值。然后,在代码的后面,如果您只想根据值是否为红色来触发一些代码,您可以执行以下操作:

if(this.colour == Colour.Red)
{
// Do red specific logic
}

Your enum Colour does not have properties for each enum value you have defined. The values Red, Green, and Blue are values that your enum could be.

You want to do something like this.colour = Colour.Red to set your colour variable to the value of red. Then, later in the code if you only wanted to trigger some code based on whether or not the value was red you could do something like:

if(this.colour == Colour.Red)
{
// Do red specific logic
}
眼眸里的快感 2024-12-15 03:37:44

你最好使用不可变的结构:

public struct Colour
{
    private readonly byte red;

    private readonly byte green;

    private readonly byte blue;

    public Colour(byte red, byte green, byte blue)
    {
        this.red = red;
        this.green = green;
        this.blue = blue;
    }

    public byte Red
    {
        get
        {
            return this.red;
        }
    }

    public byte Green
    {
        get
        {
            return this.green;
        }
    }

    public byte Blue
    {
        get
        {
            return this.blue;
        }
    }
}

你可以这样初始化它:

Shape shape = new Shape(new Colour(104, 255, 67));

You'd be best off with an immutable struct:

public struct Colour
{
    private readonly byte red;

    private readonly byte green;

    private readonly byte blue;

    public Colour(byte red, byte green, byte blue)
    {
        this.red = red;
        this.green = green;
        this.blue = blue;
    }

    public byte Red
    {
        get
        {
            return this.red;
        }
    }

    public byte Green
    {
        get
        {
            return this.green;
        }
    }

    public byte Blue
    {
        get
        {
            return this.blue;
        }
    }
}

You'd initialize it as such:

Shape shape = new Shape(new Colour(104, 255, 67));
反目相谮 2024-12-15 03:37:44

如果颜色是您的枚举,那么您在构造函数中需要做的就是说:

this.colour = c;

但是,我对您对“红绿和蓝由 0-255 值表示”的评论感到有点困惑和担心。大多数颜色对于红绿蓝都有一个值,所以你不需要一个枚举。

通过类比下拉框,可以最容易地想到枚举。有一组固定值,您的选择必须是其中一个,而不是其他。下拉列表后面可能有一个值,但通常您只是想知道下拉列表是否显示“红色”,而不是它后面的值是什么。这样做的原因是,通常使用枚举时,您只会说“If (colour=Colour.Red) then....”。

你所做的听起来更像是.NET Class Color(由美国人命名,如果这就是你想要的,这可能就是你没有找到它的原因)。它具有 R、G、B 属性以及一堆静态属性,这些属性将返回 Color 类的预定义实例(例如 Color.Red),将返回具有适当 RGB 集的 Color 实例。

所以我认为你可能在 Enums 上搞错了方向,可能你想要的是 Color 类。

您可能最不想要的就是拥有某种 Factory 类,它可以执行以下操作:(

public Color CreateColor(Colour colEnum)
{
    switch(colEnum)
    case Colour.Red:
    return Color.Red;
    etc.
}

对混淆名称以及使用 Color 和 Colour 表示歉意)

这可以确保您根据枚举获得颜色,并且仅获得枚举中定义的颜色都可以通过这种方式创建。

If colour is your enum then all you need to do in your constructor would be to say:

this.colour = c;

However, I'm a little confused and worried by your comment on "Red Green and Blue are represented by there 0-255 values". most colours will have a value for each of the red green and blue so you don't want just an enum.

An enum can be most easily thought of by analogy to a drop down box. There are a set of fixed values and your choice has to be one of those and no other. There may be a value behind the dropdown but usually you jsut want to know if the dropdown says "Red" or not, not what the value behind it is. The reason for this is that usually with Enums you are just going to say "If (colour=Colour.Red) then....".

What you are doing sounds a lot more like the .NET Class Color (named by the americans which might be why you didn't find it if this is what you want). This has properties for R,G,B as well as a bunch of Static properties that will return predefined instances of the Color class (eg Color.Red) would return an instance of Color with appropriate RGB set.

So I think you may have got the wrong end of the stick on Enums and possibly what you want here is the Color class.

The last thing you might be wanting is to have some kind of Factory class that does something like:

public Color CreateColor(Colour colEnum)
{
    switch(colEnum)
    case Colour.Red:
    return Color.Red;
    etc.
}

(apologies for confusing names and using Color and Colour)

This can ensure that you get colours based on your enum and only the colours defined in your enum are creatable in this way.

<逆流佳人身旁 2024-12-15 03:37:44

也许您正在寻找 类型安全 枚举。它们允许比标准枚举更丰富的行为。

public sealed class Colour
{
  public int RedComponent { get; private set;}
  public int GreenComponent { get; private set;}
  public int BlueComponent { get; private set;}

  public static readonly Colour Red = new Colour(255,0,0);

  private Colour(int red, int green, int blue)
  {
    RedComponent = red;
    GreenComponent = green;
    BlueComponent = blue;
  }
}

然后,您可以使用方法 Foo(Colour c){//Do Something with c.RedComponent etc.} 并调用 Foo(Colour.Red)

您甚至可以比较值按照正常的枚举:

Bar(Colour c)
{
  return c == Colour.Red;
}

Perhaps a type-safe enum is what you are looking for. They allow for much richer behaviour that standard enums.

public sealed class Colour
{
  public int RedComponent { get; private set;}
  public int GreenComponent { get; private set;}
  public int BlueComponent { get; private set;}

  public static readonly Colour Red = new Colour(255,0,0);

  private Colour(int red, int green, int blue)
  {
    RedComponent = red;
    GreenComponent = green;
    BlueComponent = blue;
  }
}

You can then have a method Foo(Colour c){//Do something with c.RedComponent etc.} and call Foo(Colour.Red)

You can even compare values as per a normal enum:

Bar(Colour c)
{
  return c == Colour.Red;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文