这是Enums的正确用法吗?

时间:2011-11-10 14:26:21

标签: c#

这是一个使用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();
    }
}

}

8 个答案:

答案 0 :(得分:4)

您已定义了多种Enum类型,但实际上并未使用它们。所以,在这方面,不是正确的用法。

关于使用数组 - 我没有看到它在枚举上的价值。

您传入的类型均为string,而不是Enum类型,属性类型也是如此。

正确的用法如下:

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;
     }
}

答案 1 :(得分:2)

问题是,为了改变它们需要重新编译。该应用还涉及到什么?这些值可以存储在数据库中吗?这样可以使更新更简单,并且无需重新编译应用程序即可完成。

答案 2 :(得分:0)

问题是当他们改变时你将不得不修改,编译,测试和发布你的代码。我建议如果有什么东西可能会改变,那就把它存放在可以在运行时配置的地方,例如数据库或配置文件。

答案 3 :(得分:0)

不,我会用他们应该代表的枚举来声明属性:

 public transmission transmission { get; private set; } 

答案 4 :(得分:0)

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

public bodystyle BodyStyle { get; set; }

答案 5 :(得分:0)

您列出的枚举实际上更适合作为数据并加载(如数组,词典等)。

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

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

答案 6 :(得分:0)

我不这么认为。

你正在反对Enum是什么。我认为Enum是一个整数常数族;您正在将它用作恰好转换为特定字符串的对象。

答案 7 :(得分:0)

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

如果您确实想使用枚举(并且这种情况合适),您可以这样做:

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;
     }

不是代码的完整更改,但我认为你会明白这一点。如果您最终将它们转换为字符串,那么声明枚举是没有意义的。保持枚举的方式。将它们传递给构造函数并将它们保存为您声明的枚举类型。

相关问题