类Coin具有以下属性:MoneyType, Value, Diameter
。
MoneyType是枚举:
enum CoinType
{
Cent,
Nickel,
Dime,
QuarterDollar,
HalfDollar,
Dollar,
}
每个MoneyType应具有特定的值和直径,例如Cent:Value = 1和Diameter = 19,因此如果我指定MoneyType,则应根据MoneyType自动分配Value和Diameter。不知道该怎么做。也许某种枚举元组?
例如:
enum CoinType
{
(Cent, 1, 19),
(Nickel, 10, 21),
.....
}
我只想要每个MoneyType都有特定的值和直径。
答案 0 :(得分:3)
另一种方法是实现自定义枚举。 根据您的需要修改来源。
#region Base Abstract Class
public abstract class EnumBaseType<T> where T : EnumBaseType<T>
{
protected static List<T> enumValues = new List<T>();
public readonly string Key;
public readonly int Value;
public readonly int Diameter;
public EnumBaseType(string key, int value, int diameter)
{
Key = key;
Value = value;
Diameter = diameter;
enumValues.Add((T)this);
}
protected static System.Collections.ObjectModel.ReadOnlyCollection<T> GetBaseValues()
{
return enumValues.AsReadOnly();
}
protected static T GetBaseByKey(string key)
{
foreach (T t in enumValues)
{
if (t.Key == key) return t;
}
return null;
}
}
#endregion
#region Enhanced Enum Sample
public class MoneyType : EnumBaseType<MoneyType>
{
public static readonly MoneyType Cent = new MoneyType("Cent", 1, 19);
public static readonly MoneyType Nickel = new MoneyType("Nickel", 5, 25);
public static readonly MoneyType Dime = new MoneyType("Dime", 10, 30);
public static readonly MoneyType QuarterDollar = new MoneyType("QuarterDollar", 25, 15);
public static readonly MoneyType HalfDollar = new MoneyType("HalfDollar", 50, 100);
public static readonly MoneyType Dollar = new MoneyType("Dollar", 100, 29);
public MoneyType(string key, int value, int diameter)
: base(key, value, diameter)
{
}
public static ReadOnlyCollection<MoneyType> GetValues()
{
return GetBaseValues();
}
public static MoneyType GetByKey(string key)
{
return GetBaseByKey(key);
}
}
#endregion
public class Test
{
public Test()
{
foreach (MoneyType rating in MoneyType.GetValues())
{
Console.Out.WriteLine("Key:{0} Value:{1}", rating.Key, rating.Value);
if (rating == MoneyType.Dollar)
{
Console.Out.WriteLine("This is a dollar!");
}
}
foreach (MoneyType rating in MoneyType.GetValues())
{
if (rating.Diameter == MoneyType.Nickel.Diameter)
{
Console.Out.WriteLine("This is a Nickel diameter!");
}
}
}
}
修改强>
答案 1 :(得分:1)
枚举只不过是带有名称的INT值。它只存储一个离散值。你想要使用的是一个类。
更新:
抱歉,关于我的错误。自从我使用C#以来,实际上已经有一段时间了。为了弥补这一点,我已经写出了我的意思:
enum CoinType {
Cent=1,
Nickel=5,
Dime=10,
QuarterDollar=25,
HalfDollar=50,
Dollar=100,
}
class Coin {
public CoinType type;
public int Diameter {
get {
switch (type) {
case CoinType.Cent: return 19;
case CoinType.Nickel: return 25;
case CoinType.Dime: return 20;
case CoinType.QuarterDollar: return 40;
case CoinType.HalfDollar: return 40;
case CoinType.Dollar: return 40;
}
return 0;
}
}
}
答案 2 :(得分:1)
您可以在枚举成员上添加自定义属性。
enum CoinType
{
[Value=1]
[Diameter=19]
Cent,
...
}
然后,您可以使用反射从属性中获取信息。
答案 3 :(得分:1)
您只需要从CoinType到直径的映射,以及从CoinType到value的另一个映射。两者都可以通过简单的switch语句完成。
enum CoinType
{
Cent,
Nickel,
Dime,
QuarterDollar,
HalfDollar,
Dollar,
}
static class CoinTypeExts
{
static double Diameter(this CoinType coinType)
{
switch (coinType)
{
case Cent: return 19;
etc...
}
}
static double Value(this CoinType coinType)
{
switch (coinType)
{
case CoinType.Cent: return 1.0;
etc...
}
}
}