构造函数中的枚举 - 如何?

发布于 2024-11-17 17:31:46 字数 392 浏览 2 评论 0原文

我对这个练习有一个问题:定义一个代表圆的类。常量定义的类保存 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 技术交流群。

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

发布评论

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

评论(8

新雨望断虹 2024-11-24 17:31:46
public enum Color { Black, Yellow, Blue, Green };

class Circle
{
    public const double PI = 3.14;

    private Color _color;
    int radius;

    public Circle(int radius, Color color)
    {
        this.radius = radius;
        this._color = color;
    }
}

您还可以传递颜色字符串,但随后您必须执行 Enum.Parse(enum 类型,字符串值)。

public enum Color { Black, Yellow, Blue, Green };

class Circle
{
    public const double PI = 3.14;

    private Color _color;
    int radius;

    public Circle(int radius, Color color)
    {
        this.radius = radius;
        this._color = color;
    }
}

You can also pass string of color, but then you'll have to do Enum.Parse(type of enum, string value).

逆流 2024-11-24 17:31:46

只需在类定义之外定义枚举并声明该类型的本地只读实例即可。

enum Color 
{ 
    Black, 
    Yellow, 
    Blue, 
    Green 
};

class Circle
{
    public const double PI = 3.14;
    public readonly Color color;

    int radius;

    public Circle(string colorValue, int r)
    {
        color = ( Color ) Enum.Parse( typeof( Color ), colorValue );
        radius = r;
    }
}

Just define your Enum outside of the class definition and declare a local read-only instance of the type.

enum Color 
{ 
    Black, 
    Yellow, 
    Blue, 
    Green 
};

class Circle
{
    public const double PI = 3.14;
    public readonly Color color;

    int radius;

    public Circle(string colorValue, int r)
    {
        color = ( Color ) Enum.Parse( typeof( Color ), colorValue );
        radius = r;
    }
}
很快妥协 2024-11-24 17:31:46

试试这个:

public enum ColorEnum {
    Black,
    Yellow,
    Blue,
    Green

}

public class Circle {
    public const double PI = System.Math.PI;
    public ColorEnum Color;

    public Circle(ColorEnum color,int radius)
    {
        this.radius = radius;
        this.Color = color
    } 
}

Try this:

public enum ColorEnum {
    Black,
    Yellow,
    Blue,
    Green

}

public class Circle {
    public const double PI = System.Math.PI;
    public ColorEnum Color;

    public Circle(ColorEnum color,int radius)
    {
        this.radius = radius;
        this.Color = color
    } 
}
甜尕妞 2024-11-24 17:31:46

枚举是一种类型。当您声明 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.

天涯沦落人 2024-11-24 17:31:46

您需要声明enum,然后将其用作变量类型。

public enum Color { Black, Yellow, Blue, Green };
public readonly Color myColor;

You need to declare the enum, and then use it as a variable type.

public enum Color { Black, Yellow, Blue, Green };
public readonly Color myColor;
银河中√捞星星 2024-11-24 17:31:46

尝试

 public  enum Color { Black, Yellow, Blue, Green };
class Circle
{
    public const double PI = 3.14;
    public readonly Color color;
    int radius;
    public Circle(Color color, int radius)
    {
        this.color = color;
        this.radius = radius;
    }
}

使用此代码

            Circle circle = new Circle(Color.Blue,100);

try this code

 public  enum Color { Black, Yellow, Blue, Green };
class Circle
{
    public const double PI = 3.14;
    public readonly Color color;
    int radius;
    public Circle(Color color, int radius)
    {
        this.color = color;
        this.radius = radius;
    }
}

for use

            Circle circle = new Circle(Color.Blue,100);
若言繁花未落 2024-11-24 17:31:46

使用私有字段并仅公开 getter。我还将枚举设为公共类并直接传递它:

   class Circle
{
    public const double PI = 3.14;
    private Color _color;
    int radius;
    public Circle(Color Color,int radius)
    {
        _color = Color;
        this.radius = radius;
    }

   public Color Color { return _color; }
}

public enum Color { Black, Yellow, Blue, Green }

Use a private field and only expose the getter. I'd also make the enum a public class and pass it in directly:

   class Circle
{
    public const double PI = 3.14;
    private Color _color;
    int radius;
    public Circle(Color Color,int radius)
    {
        _color = Color;
        this.radius = radius;
    }

   public Color Color { return _color; }
}

public enum Color { Black, Yellow, Blue, Green }
幽蝶幻影 2024-11-24 17:31:46

下面的怎么样

public enum Color
{
    Black, Yellow, Blue, Green
}

class Circle
{
    public const double PI = 3.14;
    public Color Color { get; private set; }

    int radius;

    public Circle( int radius,Color color)
    {
        this.radius = radius;
        this.Color = color;
    }
}

what about the below

public enum Color
{
    Black, Yellow, Blue, Green
}

class Circle
{
    public const double PI = 3.14;
    public Color Color { get; private set; }

    int radius;

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