Overview
This article delves into the enumeration of one of the new features of JAVA1.5, gives a detailed description of its concepts, applications, and aims to help learners better understand the enumeration issues in SCJP certification exams, and to use enumerations flexibly in development. This article includes the following:
I. Enumeration (enum) concept (enum is not a class)
Two. Why should there be enumerations
Three. Enumeration and single case mode
Four. Enumerations and policy patterns
I. The concept of enumeration
With regard to the concept of enumerations, we can first ask a question about whether enumerations are classes in the Java language where everything is a class object.
In addition to being unable to inherit, an enum can define a constructor, have a normal method, or even an abstract method, so you can basically consider an enum as a regular class. Now that you can think of it as a regular class, we might as well write a class mock enumeration.
case Scenarios for this article: take us to win (Mison) 's corporate philosophy and the job responsibilities of each job as an example: public class Misoner { private Misoner () {} //lecturer public static final Misoner Technical_adviser = new Misoner (); //Employment guide public static final Misoner Employment_guide = new Misoner (); //software outsourcing public static final Misoner SOFTWARE _development = new Misoner (); //Consulting Center public static final Misoner Consulting_center = new Misoner (); The class of copying code that is different from other classes is that the constructor is private and the object is in constant form and why is it so defined. It is not difficult to understand from the two point of view that the enumeration cannot be instantiated and the direct access element is enumerated by enumerating. Let's take a look at how the above code is implemented through enumerations: public enum misonerenum{ //lecturer, Careers Guidance Center, Software Outsourcing Center, consulting Center & nbsp; technical_adviser, Employment_GUIDE, Software_development, Consulting_center; It is much simpler to copy code by enumerating the code that is implemented.
Let me ask you two more questions:
1. How does the enumeration constructor call?
2. How the enumeration's abstract methods are implemented.
First look at the 1th question: package complex; public class Misoner { private Misoner (String position) { System.out.println ("Post:" + position);  } public static final Misoner Technical_ Adviser = new Misoner ("technical director"); public static final Misoner employment_guide = new Misoner ("Employment Guidance"); public static final Misoner software_development = new Misoner ("Project manager"); public static final Misoner consulting_center = new Misoner ("consultant"); } Copy code above the code is familiar, it is easy to understand, enumeration and how to implement the above code, as follows: Package complex; Public enum Misonerenum { technical_adviser ("technical director"), Employment_guide ("Employment Guide"), Software_development ( "project manager"), Consulting_center ("consultant"); private misonerenum (String position) { & nbsp System.out.println ("Post:" + position);   &NBSP} Replication code is followed by the 2nd question, and, similarly, we compare the Mock enumeration class with the enumeration: package complex; Public abstract class Misoner { private Misoner (String position) { &nbs P System.out.println ("Post:" + position);  } //defining internal class objects public static final Misoner technical_adviser = new Misoner ("technical director") { & nbsp public void work () { &nb Sp system.out.println ("lecturer provides first-class Java technology training");  &NBSp } }; public static final Misoner employment_guide = new Misoner ("Employment Guide") {  &N Bsp public void work () {  &NB Sp system.out.println ("Employment Guidance Center helps students achieve high-paying employment"); } }; public static final Misoner software_development = new Misoner ("project Manager") { &nbs p; public void work () { , system.out.println ("The Software Outsourcing Center provides the trainees with the commercial outsourcing paid combat"); } }; public static final Misoner consulting_center = new Misoner ("consultant") { public void work () {  &NB Sp system.out.println ("Consulting Center provides students with all the services for Life study"); } }; //Definition abstract method public abstract void work (); The code to copy the code and use the enum is as follows: Package complex; Public enum Misonerenum { technical_ Adviser ("technical director") { public void work () { &nbs P system.out.println (" Instructor provides first-class Java technology training ");  &NBSp }  }, Employment_guide ("Employment Guide") { & nbsp public void work () { &nb Sp system.out.println ("Employment Guidance Center helps students achieve high-paying employment"); & nbsp } Software_development ("project manager") { public void work () { &N bsp; system.out.println ("The Software Outsourcing Center provides the trainees with the commercial outsourcing paid combat"); & nbsp }   &NBSP}, Consulting_center ("consultant") { Public void work () { sys TEM.OUT.PRINTLN ("Counseling Center provides students with all the services for Life study"); } }; private misonerenum (String position) {& nbsp System.out.println ("Post:" + position); } public abstract void work (); Copy Code
By simulating the enumeration class and comparing the enumeration provided by JDK1.5, we believe that the concept of enumeration is understood, and the difficulty of enumeration is the constructor and abstract method.