Recently in the Hadoop#yarn part of the source code, read the state machine that part of the time, feel the use of ENMU is too flexible, in the concurrent programming network to translate an article, just met an article like this, quickly translated down, rose posture.Original link: http://www.javacodegeeks.com/2011/07/java-secret-using-enum-to-build-state.htmlPeter Lawrey Translator: Chen ZhenyangReviewEnm
An enum is a shorthand form of a enumeration (enumeration) that is contained in a Java.lang package. Familiar with c,c++,c#, or Pascal should have an understanding of the list, first look at an example:
Public enum Season {winter, Spring, Summer, fall}
An enum is an object that defines a set of values, and it can include 0 or more value members. It is of type
background:today started a new project, because to get some code from the previous old project, so I looked at the company before the project code, found that there are defined constant class, there are enumeration classes, and then just think about the function of the two, then what is the difference between them, so decided to understand the next enum enumeration First, Introductionenum types in Java are
name of the enumeration constant.Color c=color.red;System.out.println (c);//return Result: RED(5) ValueOf () Method: This method is relative to the ToString method and returns an enumeration constant of the specified enumeration type with the specified name.Color.valueof ("BLUE"); return Result: Color.Blue(6) Equals () method: Compares references to two enum class objects.Summarize:1. Enumenum--Int:int i =
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
JAVA enum Enumeration type, javaenum Enumeration type
1. Definitions of enumeration types:
1) The Enumeration type can be a separate class file.
/** * This enum is declared in the Season.java file.*/public enum Season { WINTER, SPRING, SUMMER, FALL}
2) It can also be defined in other classes.
public class D
JDK1.5 introduces a new type--enumeration. In Java it is a "small" function, but to my development has brought "big" convenience.
Big Brother I add my own understanding, to help you understand. usage One: Constants
Before JDK1.5, we defined constants as: public static fianl ... Now that you have enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants.
Long time no contact enumeration class, almost forget, today take a moment to summarize it. To be honest, enumerating classes can really make a lot of sense for us.Description: Enum class It contracts a scope, it can be understood that only a fixed number of objects can be generated to call the outside world, so the constructor method in the enumeration class is private type. Secondly, what is the special point of enumerating classes?Submit Code at a
In this section, let's talk a little bit about enumeration types.1. Enum type is a data typePackage Com.ray.ch04;public class Test {public static void main (string[] args) {MyType MyType = Mytype.one; System.out.println (MyType instanceof Object);}} Enum MyType {One, both, three}Output:True2. The enumeration type has rewritten the Tosting method and can be output directly.Package Com.ray.ch04;public class T
) is obviously not, and the compilation is passed, and what happens at runtime is unknown to us. This obviously does not conform to the type safety of Java programs.Next, let's consider the readability of this pattern. In most cases where enumerations are used, I need to be convenient to get string expressions of enumerated types. If you print an int enumeration constant, what we see is a set of numbers, which is of little use. We might think of using
Disclaimer: This blog is an original blog. Without consent. No reprint! The original link is http://blog.csdn.net/bettarwang/article/details/27262809.Starting with the enumeration class from Java 5, it is important to note that the class defined by the enum inherits the Java.lang.Enum class instead of the object class by default.At the same time , note that an enumeration class cannot derive subclasses (th
Test {public static void main (string[] args) {for (Color color:Color.values ()) {String label = Color.getlab El (); SYSTEM.OUT.PRINTLN (label);}}}
• enumerate nouns . enumeration of Image : Gets a specified enumeration object, as in the following example:
static t[] values () : Gets all enumerated objects in the custom enumeration, as in the following example
• Enumeration types can be used in switch, as in the following example:
• Theenum class is an abstract class that is the parent of th
In most programming languages, enum types are a common and essential type of data, and of course, Java is no exception. However, the enum enum type in Java has many unexpected uses, let's take a look at it.1. You can add variables and methods to the enumLet's look at a code
This example describes the use of an enum in Java. Share to everyone for your reference. The specific analysis is as follows:
1. Basic usage
Copy Code code as follows:
Enum Day {
SUNDAY, MONDAY, Tuesday, Wendsday, Thursday, FRIDAY, SATURDAY;
}
Enumerations are constants, so you should use uppercase.
2. Enumerations are objects
The enumera
class variable.
If necessary, you can provide some static methods that allow other programs to get an instance of the match based on the parameters.
Using an enumeration class can make your program more robust and avoid the randomness of creating objects.
However, it is cumbersome to implement enumerations by defining classes to implement them, and Java has added support for enumeration classes since JDK1.5.Entry:Java5 adds an
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.