In the C ++ era, the enumerated value type is available. It is a set of ordered key-value pairs. The enumerated type can be used to strike a balance between semantics and structuring. If ourCodeIt is very painful to define numbers or characters (strings) that need to be understood by documents everywhere. Lao Chen is learning Java recently. During project reconstruction, he needs enumeration types to optimize the code structure. What's amazing is that Java and.. NET and other languages (environments) provide direct support for enumeration types! However, I encountered a tough problem ......
In C ++/C #/Java, we need to define a simple enumerated value in the following format:
1 //C ++
2 //Pay attention to the ";" here.
3 EnumColors
4{
5Red,
6Green,
7Blue
8};
1 /// C #
2 // similar to C ++ syntax
3 Enum colors
4 {< br> 5 Red,
6 green,
7 blue
8 }
1 //Java
2 //It looks no different from C #.
3 EnumColors
4{
5Red,
6Green,
7Blue
8}
When I need to bind a specific constant to each enumerated value member, Java seems very bad and the difference is very large.
The C ++/C # enumerated values bound to constants are defined as follows:
1 /// C ++
2 Enum colors
3 {< br> 4 Red = 1 ,
5 Green = 2 ,
6 Blue = 3
7 };
1 //C #
2 EnumColors
3{
4Red =1,
5Green =2,
6Blue =3
7}
The Java enumerated values bound to constants are defined as follows:
1 // Java
2 // It looks a lot complicated!
3 Public Enum Colors
4 {
5 Red (1), Green (2), blue (3 );
6
7 Private Int _ Value;
8
9 Private Colors ( Int Value)
10 {
11 _ Value = value;
12 }
13
14 Public Int Value ()
15 {
16 Return _ Value;
17 }
18 }
Are the. NET monkeys smiling and all kinds of contempt eyes displayed here?
I believe you may have realized that the syntax of a Java enumeration member to bind a constant is very cumbersome. The code for the above example is very simple and syntactically speaking, it's also abnormal.
However, we can't just look at one side of the problem. We should consider that Java is definitely the reason for this. At least, it is not that easy to implement the following code in C ++/C.
1 // Java
2 // Old Chen said: Jiangshan is so coquetry, Java is the only one!
3 Public Enum Colors
4 {
5 Red (1, "Red"), Green (2, "green"), blue (3, "blue ");
6
7 Private Int _ Value;
8 Private String _ name;
9
10 Private Colors ( Int Value, string name)
11 {
12 _ Value = value;
13 _ Name = Name;
14 }
15
16 Public Int Value ()
17 {
18 Return _ Value;
19 }
20
21 Public String getname ()
22 {
23 Return _ Name;
24 }
25 }
At this point, the Java monkeys laughed and left speechless !. Net monkeys envy and hate!
Of course, in. net, the same function can be implemented through the attitude feature.(I will not describe it here. I will discuss this issue separately in future blog posts)And comply with the regular syntax specifications, unlike Java specially arranged such abnormal syntax format.
VariousProgramming LanguageIt is essentially a tool of human beings. Java has actually optimized the coding experience to a certain extent, isn't it? However, I hope that various languages can learn from each other. Don't fight with monkeys in various camps. Can fighting solve the problem?
Old Chen is a beginner in Java. I hope all the gods will be enlightened!