This article is a summary of the study on the web and its own little practice, thank you for your selfless sharing.
Recently, when I read a book on the basics of Java, I encountered an introduction to enumerations. Some information on the Internet, some summary of the information.
Usage One: Constants
Package meiju;/** * * @ClassName: Color * @Description: Constant usage of enumeration variables * @author Zhangjunshuai [email protected] * @date 2014-11 -16 PM 07:29:40 * */public enum Color {Red,green,blank,yellow}
Usage Two: Switch
Package meiju;/** * * @ClassName: Signal * @Description: Switch usage of enumeration * @author Zhangjunshuai [email protected] * @date 201 4-11-16 pm 07:33:32 * */enum Signal{green,yellow,red}public class Enumforswitch {Signal color = Signal.red;public void chan GE () {switch (color) {case RED:System.out.println ("Red"); Break;case YELLOW:System.out.println ("golden yellow"); Break;case GREEN : System.out.println ("green"); Break;default:system.out.println ("Last");}} /** * @param args */public static void main (string[] args) {//TODO auto-generated method stub}}
Usage Three: Methods in enumerations
Package meiju;/** * * @ClassName: Colormthod * @Description: Enumeration method * @author Zhangjunshuai [email protected] * @date 2014- 11-16 pm 07:35:09 * */enum colormthod{red ("Red", 1), Green ("green", 2), BLANK ("White", 3), YELLOW ("Yellow", 4);p rivate String name; How do I construct a private int index;//enumeration? Private Colormthod (String Name,int index) {this.name = Name;this.index = index;} Normal method public static String getName (int index) {for (Colormthod c:colormthod.values ()) {if (c.getindex () = = index) { System.out.println (c.name); return c.name;}} return null;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int GetIndex () {return index;} public void Setindex (int index) {this.index = index;}} public class Enummethod {/** * @param args */public static void main (string[] args) {colormthod.getname (1);}}
Usage Four: Overriding enumeration methods
Package meiju;/** * * @ClassName: Enummethodoverride * @Description: Override method * @author Zhangjunshuai [email protected] * @dat E 2014-11-16 pm 07:37:04 * */public class Enummethodoverride {public enum colormethod{red ("Red", 1), Green ("green", 2), BLANK (" White ", 3), Yello (" Yellow ", 4);p rivate string name;private int index;private colormethod (String name,int index) {this.name = name; This.index = index;} @Overridepublic String toString () {return this.index+ "_" +this.name;}} /** * @param args */public static void main (string[] args) {//TODO auto-generated method StubSystem.out.println (Colormeth Od. RED);}}
Usage Five: Implement Interface
Package meiju;/** * * @ClassName: Behavior * @Description: Enumeration Implementation Interface * @author Zhangjunshuai [email protected] * @date 2014-1 1-16 pm 07:39:28 * */interface behavior{void print (); String getInfo ();} Enum Colorinterface implements Behavior{red ("Red", 1), Green ("green", 2), BLANK ("White", 3), YELLOW ("Yellow", 4);p rivate String name; private int index;private colorinterface (String name,int index) {this.name = Name;this.index = index;} Public String GetInfo () {return this.name;} public void print () {System.out.println (this.index+ ":" +this.name);}} public class Enuminterface {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stub ColorInterface.RED.print ();}}
Usage Six: Enumerating the use of collections
Package Meiju;import java.util.enummap;import java.util.enumset;/** * * @ClassName: Enumlighttest * @Description: Enumeration Traversal * @author Zhangjunshuai [email protected] * @date 2014-11-16 PM 07:41:13 * */public class Enumlighttest {//1. Defining enumeration Types Publ IC enum LIGHT{//2. Use the parameters of the construction parameter Red (1), GREEN (3), YELLOW (2);//define the private variable private int ncode;//constructor, the enumeration type can only be private private light (int _ NCode) {this.ncode = _ncode;} Public String toString () {return string.valueof (This.ncode);}} /** * @param args */public static void main (string[] args) {//1. Traversal enumeration Type SYSTEM.OUT.PRINTLN ("Demonstrates traversal of enumeration type ..."); Testtranversalenum (); The use of the//2.enummap object System.out.println ("demonstrates the use and traversal of Enummap objects ..."); Testenummap ();//3. Demonstrates the use of the Enumset System.out.println ("Demo Enumset object usage and traversal ..."); Testenumset (); /** * Demo Enumeration type traversal */private static void Testtranversalenum () {light[] alllight = Light.values (); for (light alight:alllight) { SYSTEM.OUT.PRINTLN ("Current lamp Name:" +alight.name ()); SYSTEM.OUT.PRINTLN ("Current lamp Ordinal:" +alight.ordinal ()); System.out.println ("Current light:" +alight);}} /** * Demo Enummap use, Enummao and hashThe use of the map is similar, except that the key value is the enumeration type */private static void Testenummap () {//1. Demo defines the Enummap object, and the constructor for the Enummap object requires arguments to be passed in, The default is the type of key class enummap<light,string> Currenummap = new Enummap<light, string> (Light.class); CurrEnumMap.put ( Light.red, "red Light"), Currenummap.put (Light.green, "green light"), Currenummap.put (Light.yellow, "yellow"); Light.values ()) {System.out.println ("[key=" +alight.name () + ", value=" +currenummap.get (alight) + "]");}} /** * Demonstrates how enumset is used, Enumset is an abstract class that gets a type of enum type content * * can use AllOf method */private static void Testenumset () {Enumset<light > currenumset = Enumset.allof (Light.class); for (light Alightsetelement:currenumset) {System.out.println (" The data in the current Enumset is: "+alightsetelement);}}}
Usage VII: Organizing enumerations with interfaces
Public interface Food {enum Coffee implements Food{black_coffee,decaf_coffee,latte,cappuccino}enum dessert implements Food{fruit, CAKE, GELATO}}
Enumerations can reduce the definition of a variable relative to final static, and the use of it in switch facilitates the execution of code, while enumerations can be customized with built-in methods, and enumerations can use traversal methods. Although enumerations have some advantages, enumerations are not inherited.
Reference:
http://www.iteye.com/topic/1116193
http://blog.csdn.net/p106786860/article/details/11516269
Summary of the enumeration of Java SE Learning