构造函数中的枚举 - 如何?
我对这个练习有一个问题:定义一个代表圆的类。常量定义的类保存 pi 的值,只读定义的变量保存圆圈的颜色。可能的颜色在枚举中定义。定义类的变量来保存圆的半径 以及计算对象的周长和面积的函数。 这就是我所做的:
class Circle
{
public const double PI = 3.14;
public readonly enum color { Black, Yellow, Blue, Green };
int radius;
public Circle(string Color,int radius)
{
this.radius = radius;
}
}
我不知道如何将枚举选择放入构造函数中。 谢谢你的帮助。
I have a problem with this exercise: Define a class that represent a circle. Constant defined class that holds the value of pi, and a variable defined in readonly holding the color of the circle. The possible colors are defined in enum. Variables defined class to hold the radius of the circle
And functions that calculate the perimeter and area of the object.
That's what I've done:
class Circle
{
public const double PI = 3.14;
public readonly enum color { Black, Yellow, Blue, Green };
int radius;
public Circle(string Color,int radius)
{
this.radius = radius;
}
}
I don't know how can I put the enum selection in the constructor.
Thanks for helping.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您还可以传递颜色字符串,但随后您必须执行 Enum.Parse(enum 类型,字符串值)。
You can also pass string of color, but then you'll have to do Enum.Parse(type of enum, string value).
只需在类定义之外定义枚举并声明该类型的本地只读实例即可。
Just define your Enum outside of the class definition and declare a local read-only instance of the type.
试试这个:
Try this:
枚举是一种类型。当您声明 Enum 时,您实际上是在程序中定义一个新类型。因此,如果您想将 Enum 值作为参数传递,那么您需要声明该类型的参数。
An Enum is a type. When you declare an Enum, you are actually defining a new type within your program. Therefore, if you want to pass an Enum value in as a parameter, then you need to declare a parameter of that type.
您需要声明
enum
,然后将其用作变量类型。You need to declare the
enum
, and then use it as a variable type.尝试
使用此代码
try this code
for use
使用私有字段并仅公开 getter。我还将枚举设为公共类并直接传递它:
Use a private field and only expose the getter. I'd also make the enum a public class and pass it in directly:
下面的怎么样
what about the below