C# 通过Attitude特性来实现,相对方便些
namespace Extensions { /// <summary> /// 定义枚举扩展类 /// </summary> public static class EnumExtensions { /// <summary> /// 获取枚举变量值的 Description 属性 /// </summary> /// <param name="obj">枚举变量</param> /// <returns>如果包含 Description 属性,则返回 Description 属性的值,否则返回枚举变量值的名称</returns> public static string GetDescription(this object obj) { return GetDescription(obj, false); } /// <summary> /// 获取枚举变量值的 Description 属性 /// </summary> /// <param name="obj">枚举变量</param> /// <param name="isTop">是否改变为返回该类、枚举类型的头 Description 属性,而不是当前的属性或枚举变量值的 Description 属性</param> /// <returns>如果包含 Description 属性,则返回 Description 属性的值,否则返回枚举变量值的名称</returns> public static string GetDescription(this object obj, bool isTop) { if (obj == null) { return string.Empty; } try { var enumType = obj.GetType(); DescriptionAttribute dna; if (isTop) { dna = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute)); } else { var fi = enumType.GetField(Enum.GetName(enumType, obj)); dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute)); } if (dna != null && string.IsNullOrEmpty(dna.Description) == false) { return dna.Description; } } catch { } return obj.ToString(); } /// <summary> /// 获取枚举变量值的 ExtendProperty 属性 /// </summary> /// <param name="obj">枚举变量</param> /// <returns>如果包含 ExtendProperty 属性,则返回 ExtendProperty 属性的值,否则返回枚举变量值的名称</returns> public static string GetExtendProperty(this object obj) { if (obj == null) { return string.Empty; } try { Type _enumType = obj.GetType(); ExtendPropertyAttribute dna = null; FieldInfo fi = _enumType.GetField(Enum.GetName(_enumType, obj)); dna = (ExtendPropertyAttribute)Attribute.GetCustomAttribute( fi, typeof(ExtendPropertyAttribute)); if (dna != null && !string.IsNullOrEmpty(dna.ExtendProperty)) return dna.ExtendProperty; } catch { } return obj.ToString(); } } [AttributeUsage(AttributeTargets.Field)] public class ExtendPropertyAttribute : Attribute { public string ExtendProperty; public ExtendPropertyAttribute(string extendProperty) { this.ExtendProperty = extendProperty; } } } static class Program { public enum EnumTest { [Description("")] Undefinition, [Description("红色")] Red, [Description("绿色")] Green, [Description("蓝色")] Blue = 4 } private static void Main(string[] args) { foreach (EnumTest enumTest in Enum.GetValues(typeof(EnumTest))) { Console.WriteLine((int)enumTest + ":" + enumTest); } Console.WriteLine("名称:" + EnumTest.Blue.GetDescription()); Console.WriteLine("值:" + (int)EnumTest.Blue); Console.ReadLine(); } }
JAVA 编码的时候繁琐些,每次都需要定义 get,set 方法,下面是目前找到的相对好使,常用的方法
public enum EnumTest { Undefinition(0,""),Red(1, "红色"), Green(2, "绿色"), Blue(4, "蓝色"); private int _value; private String _name; EnumTest(int value, String name) { _value = value; _name = name; } public int getValue() { return _value; } public String getName() { return _name; } } public static void main(String[] args) throws Exception { EnumSet<EnumTest> weekSet = EnumSet.allOf(EnumTest.class); for (EnumTest day : weekSet) { System.out.println(day.getName()); System.out.println(day.getValue()); } System.out.println("名称:"+EnumTest.Blue.getName()); System.out.println("顺序值:"+EnumTest.Blue.ordinal()); System.out.println("自定义值:"+EnumTest.Blue.getValue()); }