Enumeration
Enumeration overview
is to list the value of the variable one by one, and the value of the variable is limited to the range of values enumerated. Example: only 7 days a week, only 12 months a year.
Recall a singleton design pattern: A singleton class is a class with only one instance
So many examples are a class with multiple instances, but not an infinite number of instances, but a finite number of instances. This can be an enumeration class.
The custom enumeration class.
First edition: Non-parametric construction
1 Public classDirection {2 //Create several instances3 Public Static FinalDirection FRONT =NewDirection ();4 Public Static FinalDirection BEHIND =NewDirection ();5 Public Static FinalDirection left =NewDirection ();6 Public Static FinalDirection right =NewDirection ();7 8 //constructs the private, the others cannot create indefinitely9 PrivateDirection () {Ten } One}
Second Edition: with parametric construction
1 Public classDirection2 {2 //Create several instances3 Public Static FinalDirection2 FRONT =NewDirection2 ("front");4 Public Static FinalDirection2 BEHIND =NewDirection2 ("after");5 Public Static FinalDirection2 left =NewDirection2 ("left");6 Public Static FinalDirection2 right =NewDirection2 ("Right");7 8 //constructs the private, the others cannot create indefinitely9 //Private Direction2 () {Ten // } One A //add member variables and remove parameterless constructs - PrivateString name; - the PrivateDirection2 (String name) { - This. Name =name; - } - + PublicString GetName () { - returnname; + } A}
Third version: Constructed method and parametric structure
1 Public Abstract classDirection3 {2 //Create several instances3 4 //because objects cannot be instantiated in an abstract class, that is not the case5 //Public static final Direction3 FRONT = new Direction3 ("front");6 //However, you can use the anonymous internal method, as follows:7 //This is to rewrite the new Direction3 ("former") {} This subclass object to the abstract method: Show (), and give the Father Direction3 FRONT. 8 Public Static FinalDirection3 FRONT =NewDirection3 ("front") {9 @OverrideTen Public voidShow () { OneSystem.out.println ("front"); A } - - }; the Public Static FinalDirection3 BEHIND =NewDirection3 ("after") { - @Override - Public voidShow () { -System.out.println ("after"); + } - + }; A Public Static FinalDirection3 left =NewDirection3 ("left") { at @Override - Public voidShow () { -System.out.println ("left"); - } - - }; in Public Static FinalDirection3 right =NewDirection3 ("Right") { - @Override to Public voidShow () { +System.out.println ("Right"); - } the * }; $ Panax Notoginseng //constructs the private, the others cannot create indefinitely - //Private Direction2 () { the // } + A //add member variables and remove parameterless constructs the PrivateString name; + - PrivateDirection3 (String name) { $ This. Name =name; $ } - - PublicString GetName () { the returnname; - }Wuyi the //Adding abstract methods - Public Abstract voidShow (); Wu}
Finally, the test class:
1 Public classDirectiondemo {2 Public Static voidMain (string[] args) {3 4 //Print the first way5Direction d =Direction.front;6System.out.println (d);//Print out Address value7System.out.println ("------------------------------------");8 9 //Print the second wayTenDirection2 D2 =Direction2.front; OneSystem.out.println (D2);//Print out Address value ASystem.out.println (D2.getname ());//before -D2 =Direction2.right; - System.out.println (D2); the System.out.println (D2.getname ()); -System.out.println ("------------------------------------"); - - //print a third way +Direction3 D3 =Direction3.front; -SYSTEM.OUT.PRINTLN (D3);//Print out Address value +System.out.println (D3.getname ());//before A //It is supposed that abstract classes cannot invoke methods, but this can be adjusted. at //because new Direction3 ("front") {} This subclass object overrides the abstract method: Show () method, and run look to the right, there is show () - d3.show (); -D3 =Direction3.left; - System.out.println (D3); - System.out.println (D3.getname ()); - d3.show (); in } -}
Overview of enumerations for new features of Java 28-4 JDK5 and custom enumeration classes