冷眼看 java 的枚举
看看这样一段 C# 代码实现了什么?
1 public abstract class EnumBase<T> where T : EnumBase<T>
2 {
3 public int Value { get; protected set; }
4
5 public string Code { get; protected set; }
6
7 public string Name { get; protected set; }
8
9 /// <summary>
10 /// Initializes a new instance of the EnumBase class.
11 /// </summary>
12 protected EnumBase(int value, string code, string name)
13 {
14 Value = value;
15 Code = code;
16 Name = name;
17 }
18
19 protected static T ValueOf(IEnumerable<T> values, int value)
20 {
21 foreach (T item in values)
22 {
23 if (item.Value == value)
24 {
25 return item;
26 }
27 }
28
29 return null;
30 }
31
32 protected static T ValueOf(IEnumerable<T> values, string name)
33 {
34 foreach (T item in values)
35 {
36 if (item.Name.Equals(name, StringComparison.OrdinalIgnoreCase)
37 || item.Code.Equals(name, StringComparison.OrdinalIgnoreCase))
38 {
39 return item;
40 }
41 }
42
43 return null;
44 }
45
46 public override string ToString()
47 {
48 return Code;
49 }
50
51 public override int GetHashCode()
52 {
53 return Code.GetHashCode();
54 }
55 }
56
57
58 public class CompScope : EnumBase<CompScope>
59 {
60 public static CompScope CommonComponent = new CompScope(0, "CommonComponent", "一般组件");
61
62 public static CompScope SystemComponent = new CompScope(1, "SystemComponent", "系统组件");
63
64 public static CompScope ShareComponent = new CompScope(2, "ShareComponent", "ShareComponent");
65
66 private static CompScope[] values = new CompScope[] { CommonComponent, SystemComponent, ShareComponent };
67
68 public static IEnumerable<CompScope> Values
69 {
70 get
71 {
72 return values;
73 }
74 }
75
76 public static CompScope ValueOf(int value)
77 {
78 return ValueOf(values, value);
79 }
80
81 public static CompScope ValueOf(string name)
82 {
83 return ValueOf(values, name);
84 }
85
86 /// <summary>
87 /// Initializes a new instance of the CompScope class.
88 /// </summary>
89 private CompScope(int value, string code, string name) : base(value, code, name)
90 {
91 }
92 }
93
2 {
3 public int Value { get; protected set; }
4
5 public string Code { get; protected set; }
6
7 public string Name { get; protected set; }
8
9 /// <summary>
10 /// Initializes a new instance of the EnumBase class.
11 /// </summary>
12 protected EnumBase(int value, string code, string name)
13 {
14 Value = value;
15 Code = code;
16 Name = name;
17 }
18
19 protected static T ValueOf(IEnumerable<T> values, int value)
20 {
21 foreach (T item in values)
22 {
23 if (item.Value == value)
24 {
25 return item;
26 }
27 }
28
29 return null;
30 }
31
32 protected static T ValueOf(IEnumerable<T> values, string name)
33 {
34 foreach (T item in values)
35 {
36 if (item.Name.Equals(name, StringComparison.OrdinalIgnoreCase)
37 || item.Code.Equals(name, StringComparison.OrdinalIgnoreCase))
38 {
39 return item;
40 }
41 }
42
43 return null;
44 }
45
46 public override string ToString()
47 {
48 return Code;
49 }
50
51 public override int GetHashCode()
52 {
53 return Code.GetHashCode();
54 }
55 }
56
57
58 public class CompScope : EnumBase<CompScope>
59 {
60 public static CompScope CommonComponent = new CompScope(0, "CommonComponent", "一般组件");
61
62 public static CompScope SystemComponent = new CompScope(1, "SystemComponent", "系统组件");
63
64 public static CompScope ShareComponent = new CompScope(2, "ShareComponent", "ShareComponent");
65
66 private static CompScope[] values = new CompScope[] { CommonComponent, SystemComponent, ShareComponent };
67
68 public static IEnumerable<CompScope> Values
69 {
70 get
71 {
72 return values;
73 }
74 }
75
76 public static CompScope ValueOf(int value)
77 {
78 return ValueOf(values, value);
79 }
80
81 public static CompScope ValueOf(string name)
82 {
83 return ValueOf(values, name);
84 }
85
86 /// <summary>
87 /// Initializes a new instance of the CompScope class.
88 /// </summary>
89 private CompScope(int value, string code, string name) : base(value, code, name)
90 {
91 }
92 }
93
是不是和 Java 的 Enum 很象,而且,很显然的,这段代码用Java写也是这样的效果。
当然,Java 中直接声明 Enum,不需要显式的声明继承自 Enum<XXXX>,那些 static 的枚举成员,只需要声明一个名称,而且不需要声明 values 数组和 ValueOf 函数,这些都由 java 编译器自动处理了。
可是,哦,这不就是个语法糖么,当 java 的那些家伙口口声声说 C# 3.0 lamda表达式,对象初始化等特性不就是一些语法糖的时候,不知道他们是否还记得,他们所谓的 枚举 其实不过也只是一个更大语法糖而已。
浙公网安备 33010602011771号