在类构造函数中初始化枚举的正确方法
我可能以错误的方式看待枚举,但想确保我在如何使用它们方面拥有正确的理论。
假设我们有一个名为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在这种情况下,您可能希望 Color 是
struct
而不是枚举。在 C# 中,枚举是单值构造,但有三个值(红色、绿色和蓝色)。我可能会这样做:然后当您创建形状对象时:
编辑:正如克里斯在评论中指出的那样,您可以简单地使用
System.Drawing.Color 框架提供的结构体。上面的例子可以简化为:
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:And then when you're creating your shape objects:
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:枚举非常类似于关系数据库中的类型(或关联)表。它是一组选项列表,因此您可以限制一个值。如果您不太熟悉关系数据库,则可以将枚举视为“选择列表”。枚举为我们提供了一个小的、有限的变量选择列表,而基础类型(int、byte 等)则要大得多。
完成您正在尝试的操作的正常方法是测试枚举值,然后设置您的对象:
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:
this.colour
属于Colour
类型,并且只能采用三个值之一。将Colour
视为类似于Integer
的类型,但它只能采用三个值之一,而Integer
可以采用中的任何一个值>Integer.MIN_VALUE..Integer.MAX_VALUE
范围。如果您尝试从中构建 RGB 颜色,那么这不是正确的方法。在这种情况下,您正在寻找一个名为
Colour
的类,它的红色、绿色和蓝色
分量具有不同的值。this.colour
is of typeColour
and can take only one of the three values. Think ofColour
as a type likeInteger
but it can take one of only three values whileInteger
can take any one of the values inInteger.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 namedColour
which has different values for itsred, green and blue
components.您的枚举
Colour
没有您定义的每个枚举值的属性。 Red、Green 和 Blue 值是您的枚举可能的值。您想要执行类似
this.colour = Colour.Red
的操作,将colour
变量设置为红色值。然后,在代码的后面,如果您只想根据值是否为红色来触发一些代码,您可以执行以下操作: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 yourcolour
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:你最好使用不可变的结构:
你可以这样初始化它:
You'd be best off with an immutable struct:
You'd initialize it as such:
如果颜色是您的枚举,那么您在构造函数中需要做的就是说:
但是,我对您对“红绿和蓝由 0-255 值表示”的评论感到有点困惑和担心。大多数颜色对于红绿蓝都有一个值,所以你不需要一个枚举。
通过类比下拉框,可以最容易地想到枚举。有一组固定值,您的选择必须是其中一个,而不是其他。下拉列表后面可能有一个值,但通常您只是想知道下拉列表是否显示“红色”,而不是它后面的值是什么。这样做的原因是,通常使用枚举时,您只会说“If (colour=Colour.Red) then....”。
你所做的听起来更像是.NET Class Color(由美国人命名,如果这就是你想要的,这可能就是你没有找到它的原因)。它具有 R、G、B 属性以及一堆静态属性,这些属性将返回 Color 类的预定义实例(例如 Color.Red),将返回具有适当 RGB 集的 Color 实例。
所以我认为你可能在 Enums 上搞错了方向,可能你想要的是 Color 类。
您可能最不想要的就是拥有某种 Factory 类,它可以执行以下操作:(
对混淆名称以及使用 Color 和 Colour 表示歉意)
这可以确保您根据枚举获得颜色,并且仅获得枚举中定义的颜色都可以通过这种方式创建。
If colour is your enum then all you need to do in your constructor would be to say:
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:
(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.
也许您正在寻找 类型安全 枚举。它们允许比标准枚举更丰富的行为。
然后,您可以使用方法
Foo(Colour c){//Do Something with c.RedComponent etc.}
并调用Foo(Colour.Red)
您甚至可以比较值按照正常的枚举:
Perhaps a type-safe enum is what you are looking for. They allow for much richer behaviour that standard enums.
You can then have a method
Foo(Colour c){//Do something with c.RedComponent etc.}
and callFoo(Colour.Red)
You can even compare values as per a normal enum: