这是枚举的正确用法吗?

Is this the correct use of Enums?

提问人:Aaron 提问时间:11/10/2011 更新时间:11/10/2011 访问量:209

问:

这是使用 enmus 的好案例吗? 还是使用数组会更好? 这里的值不会改变,也许一年一次。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{

public enum transmission
{
    Manual,
    NonSynchronous,
    Automatic,
    SemiAutomatic,
    Continuously,
    Infinitely,
    Electric,
    Hydrostatic,
    Hydrodynamic,
}

public enum bodystyle
{
    Convertable,
    Hatchback,
    Sports,
    Sedan
}
public enum carcolors
{
    Red,
    Blue,
    Yellow,
    White,
    Black,
    Green
}
public enum fueltype
{   
    Biofuels,
    FossilFuels,
    Nuclear,
    Fission,
    Fusion
}

public class Car
{
       public Car(String cName, double cMaxSpeed, String cTransmission, String cBodystyle, String cColors, String cFueltype) {
         carname = cName;
         transmission = cTransmission;
         bodystyle = cBodystyle;
         colors = cColors;
         fueltype = cFueltype;
         maxspeed = cMaxSpeed;
     }
     public string carname
     {
         get;
         set;
     }
     public string transmission
     {
         get;
         private set;
     }
     public string bodystyle
     {
         get;
         private set;
     }
     public string colors
     {
         get;
         private set;
     }
     public string fueltype
     {
         get;
         private set;
     }

     public void carInfo()
     {
         Console.WriteLine("------------------------------------");
         Console.WriteLine("Car Name:         " + this.carname);
         Console.WriteLine("Car Transmission: " + this.transmission );
         Console.WriteLine("Car Bodystyle:    " + this.bodystyle);
         Console.WriteLine("Car Colors:       " + this.colors);
         Console.WriteLine("Car Fueltype:     " + this.fueltype);
         Console.WriteLine("Car MaxSpeed:     " + this.maxspeed);
         Console.WriteLine("------------------------------------");

     }


}

public class Program
{
    static void Main(string[] args)
    {
        Car nissan = new Car("Lamborgini", 255, Convert.ToString(transmission.Automatic), Convert.ToString(bodystyle.Sports), Convert.ToString(carcolors.Red), Convert.ToString(fueltype.Biofuels));
        nissan.carInfo();
    }
}

}
c#

评论

0赞 Adam Houldsworth 11/10/2011
如果你使你的标题与你的问题内容相匹配,这将有助于提高答案的质量。标题询问这是否是正确的用法,但事实并非如此,问题询问这是否是最佳行动方案与查找列表,这是值得商榷的。

答:

4赞 Oded 11/10/2011 #1

您定义了多个类型,但实际上并未使用它们。因此,在这方面,不是一个正确的用法。Enum

关于使用数组 - 我看不出枚举的价值。

您传入的类型是全部类型,而不是属性类型。stringEnum

正确的用法如下所示:

public enum BodyStyle
{
    Convertable,
    Hatchback,
    Sports,
    Sedan
}

public class Car
{
  public Car(String cName, BodyStyle cBodyStyle)
  {
     carname = cName;
     this.BodyStyle = cBodyStyle;
  }


     public string carname
     {
         get;
         set;
     }

     public BodyStyle BodyStyle
     {
         get;
         private set;
     }
}

评论

0赞 James Johnson 11/10/2011
@PaulJackson:为什么会发表评论?这是一个简短的答案,但它是正确的。
0赞 Adam Houldsworth 11/10/2011
@JamesJohnson 这不是一个答案,而是一个观察。
0赞 Adam Houldsworth 11/10/2011
@Oded 我不认为它回答了在这种情况下他选择枚举而不是查找列表是否是一个好问题。虽然你是对的,但我相信它偏离了主要问题。
0赞 Oded 11/10/2011
@AdamHouldsworth - 我认为主要问题是他对 s 的用法是否正确。事实并非如此。Enum
0赞 Adam Houldsworth 11/10/2011
@Oded 标题与问题不符。我倾向于听问题而不是标题。我没有太注意标题。-(-1)...哈哈
2赞 kevingreen 11/10/2011 #2

问题是,为了更改它们,需要重新编译。此应用程序还与什么有关?这些值可以存储在数据库中吗?这将使更新它变得更加简单,并且您可以在不重新编译应用程序的情况下进行更新。

评论

0赞 kevingreen 11/10/2011
我想我已经读过那篇文章了,如果它是我想到的那篇。“软编码地狱”。没错,你可以通过将数据定义分散到各处来破坏一个完全可读的程序。但有时编译代码并部署到生产环境中可能会很麻烦,而快速更新数据库或配置文件会更简单。
0赞 dsolimano 11/10/2011
事实上。但另一方面,当我没有像代码发布那样认真对待它和 QA 时,我已经用快速更新数据库搞砸了许多生产环境。
0赞 Andy Rose 11/10/2011 #3

问题是,当它们发生变化时,您将不得不修改、编译、测试和发布您的代码。我建议如果某些东西可能会发生变化,然后将其存储在可以在运行时配置的地方,例如数据库或配置文件。

0赞 Fischermaen 11/10/2011 #4

不,我会用它们应该表示的枚举来声明属性,如下所示:

 public transmission transmission { get; private set; } 
0赞 James Johnson 11/10/2011 #5

看起来不像是在代码中使用枚举。属性类型应与创建的枚举相对应:

public bodystyle BodyStyle { get; set; }
0赞 iCollect.it Ltd 11/10/2011 #6

您列出的枚举实际上更适合作为数据并加载(放入数组、字典等)。

枚举的优点是它们实际上是数值,因此,如果您需要对任何值进行显式测试,则应考虑使用 TT 模板从数据生成枚举。

基本上,如果它们不是以编程方式使用(即测试的值),那么它们可能不是真正的枚举:)

0赞 satnhak 11/10/2011 #7

我不这么认为。

你正在与枚举是什么作斗争。我认为枚举是一个整数常量族;您将其用作恰好转换为特定字符串的对象。

0赞 pyrocumulus 11/10/2011 #8

我不明白为什么你声明枚举而只使用它们的字符串组件。在这种情况下,您不妨使用字符串数组。

如果你确实想使用枚举(是的,这种情况是合适的),你可以这样做:

public Car(String cName, double cMaxSpeed, transmission cTransmission, bodystyle cBodystyle, carcolors cColors, fueltype cFueltype) {
         carname = cName;
         transmission = cTransmission;
         bodystyle = cBodystyle;
         colors = cColors;
         fueltype = cFueltype;
         maxspeed = cMaxSpeed;
     }

     public transmission transmission
     {
         get;
         private set;
     }
     public bodystyle bodystyle
     {
         get;
         private set;
     }
     public carcolors colors
     {
         get;
         private set;
     }
     public fueltype fueltype
     {
         get;
         private set;
     }

不是完全改变你的代码,但我想你会明白的。如果最终只是将枚举转换为字符串,那么声明枚举是没有意义的。保持枚举保持原样。将它们传递到构造函数中,并将它们另存为声明的枚举类型。