Most recent enumeration types are more frequently used
DK1.5 introduces a new type--enumeration. In Java it is a "small" function, but to my development has brought "big" convenience.
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.
Jav
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.
public enum Color {
RED, GREEN, BLANK, Yellow
} Usage Two: Switch
The switch statement before JDK1.6 only supports the Int,char,enum type, and using enumerations makes our c
//print a specific value; Public classDemo1 { Public voidTest () {print (GRADE.A); } Public voidPrint (Grade Grade) {//print a specific value;String value=Grade.localvalue (); System.out.println (value); } /** Method One static class Grade{public Grade () {} private static final Grade A =new Grade (); private static final Grade b=new Grade (); private static final Grade c=new Grade (); private static final Grade d=new Grade (); }*/ //Method Two enumgr
Extend the content of the introduction to Programming (Java) 3.2.6 to introduce the enumin a comprehensive way.1. Basic usageThe keyword enum introduced in Java 5, which defines the enumeration type (enumerated type).public enum direction{ Up,down,left,right}☆ effectively reduce the declaration of public static fina
The Java enum is singleton class (only one instance). Java uses compilers and JVMs to prevent an enum from generating more than one class: You cannot use the new, Clone (), De-serialization, and Reflection APIs to produce an enum's instance.
So there are other ways to create an enumerated object by reflection:
For exam
In Java, serialization of an enum type differs from the serialization of other object types, to see what is different today. Let's look at how the enum we defined in Java is going to look like after being compiled.
Java code:
Java
Copy from other people there myeclipse Java engineering, open a look marked red a large, careful look, it is not recognized enum keyword, this is a bit embarrassing.
I built a Java project myself and tested it, if I chose JavaSE-1.6 when I was building a new project:
is to identify the enum keyword, but if I choose
JDK1.5 introduces a new type--enumeration. In Java, although it is a "small" function, but to my development has brought "big" convenience.Usage One: constant.Before JDK1.5, we defined constants: public static fianl ..... Now, with enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants. Public enum Color { RED, GREEN, BLANK, YELLOW}Si
attributes of the class type, such as construction method, member variable, member method, etc. This is the enumeration type not only has to distinguish the different categories of functions, and even can add their own methods for different types, of course, to implement an interface before the line, and implement some of these types of public methods, so the enumeration type to represent different types, it is more flexible than with different static constants.The difference between an enumera
PackageCn.edu.shu.web.util;/** * * @author Tkpad [ Email protected] * @version V1.0.0 * */ Public enumFileType {WORD (1), TXT (2), ANNO (3);//Note that the sentence must be placed in the first line Private intType//define the variable used in the database, or the value of the enumeration variable //Use a constructor to assign a value to a defined enumeration variable Private FileType(intType) { This. type = type; }//By overwrite the ToString method, you can get the value of the enume
After Java1.5, the enumeration of new features was introduced, before Java had two ways of defining new types: Classes and interfaces. But in special cases, we can't satisfy our needs, for example, we define a color class, which can only have red,blue,green three values and is not valid for other values. Before we introduce enumerations, we typically use them to privatize their construction methods, and then instantiate three constants inside them, 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.