Welcome reprint, Reprint please specify the Source: http://it.zhutibo.com/action/article1227.htm
Learn the first question of enumerations: Why not use the static final field instead
I think almost every person learning the Java language in the study of the enumeration is one of the first questions: Why not use the static final field instead. , the question is that you have groped for the most important function of the enumeration type: to replace the variable direct quantity with a uniformly named constant, which is undoubtedly the most important function of the enumeration, and in most cases, a static final is sufficient, So n many well-known source code we will often see the static final field to define a series of constants; then why use enumerations? There are several reasons: an enumerated element is an instance, so it is also a type, and the relative field is more extensible: extended from paragraph, method, traversal, and the Declaration class of an enumeration is also a type, which provides a lot of methods for enumeration, such as values (), relative to the class that the field affirms. Enumeration access, comparison is both performance (ordinal), and readability (name);
Enumeration in Java is a grammar that requires rote memorization and a design that needs to be well understood.
(experience is greater than commentary)
Principle
Enumeration provides a set of specific, compile-time syntax only (which is translated at run-time to normal), and provides a set of tailored syntax for the enum feature. Used to provide fast, and relatively scalable functionality on the specific occasion of the enumeration, without the need for a developer to implement it again. Enum this thing, as opposed to class, the former is only one thing at compile time, the enum is translated into class at Run-time, in other words, no Enum,java language still retains the same powerful function, only when dealing with the requirements of "enumeration", the programmer may be more headaches.
Enumerate this thing let me remember a childhood one thing, buy a fan; at that time, I remember the TV often put on Zheng Banqiao's TV series, Zheng Banqiao chest often have a write "Zheng Banqiao" three characters of the analysis fan, so popular at that time the demolition fan wind, remember that there is a small shop in the town of folding fans sell very well, and the general store is not the same, The boss selling the folding fan is also quite good at calligraphy (at least we looked like a model at the time), so every time we buy a fan, we ask the boss to help us write our name on the fan, so the boss's fan "sell" is quite successful; Other enumeration this function, It is the same as Sun Boss (now Oracle) in the promotion of Java this fan to write a name, although even if the boss does not write, we can write, but how to write, perhaps better. Maybe even worse. But in general, the boss has a certain level of writing, and we certainly can be labor-saving.
Grammatical features
A word.
Enum A{biga, smalla{String extfield; void DoSomething () {};}, Largea}
If we do this in class syntax, it's equivalent to (just the equivalent, to let you know how complex the functionality of implementing enumerations with the original syntax is):
Final class A extends enum{
The fields, methods, such as values () that the compiler generates for us;
Related constructors
Static final A Biga = new A ("Biga", 0);
STAITC final A Smailla = new A ("Smalla", 1) {
void DoSomething () {}
}
Static final A Biga = new A ("Largea", 3);
}
Hide the difference between an attribute element and an element with the Odinal field, the print mark in which the value starts at 0 to increment the element after the name field enumeration element is compiled with the static final decorated enumeration declaration class at least final decorated enumeration declaration class if it is a member class of other classes, Then the default will be modified by the static modifier, to become statically member class enumeration at compile time will automatically add some "methods", such as values (); Enumeration class automatically inherits Enum<e extend Enum<e>> class at compile time, so you cannot inherit the syntax of other class enumerations. You must understand