Several commonly used enumerations (enum) methods in Java

Source: Internet
Author: User

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.

Java code 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 code more readable.

  Java code   enum signal {       green, yellow, red   }   public class trafficlight {       signal  color = Signal.RED;       public void change ()  {            switch  (color)  {            case RED:                color = Signal.GREEN;                break;           case  yellow:               color =  signal.red;               break;            case green:                color = Signal.YELLOW;                break;           }        }  }    usage Three: Add a new method to the enumeration

If you intend to customize your own method, you must add a semicolon at the end of the sequence of enum instances. and Java requirements must first define an instance of an enum.

  Java code   public enum color {       red ("Red",  1),  green ("green",  2),  blank ("white",  3),  yello ("Yellow",  4);        //  member variables        private String name;        private int index;       //  Construction Method         private color (string name, int index)  {            this.name = name;            this.index = index;       }        //  General method        public static string getname (int  index)  {           for  (color c :  color.values ()) &NBsp {               if  (C.getindex ()  == index)  {                    return c.name;                }           }            return null;       }        // get set  Methods        public String  GetName ()  {           return name;       }       public void setname (String name)  {           this.name = name;      &nBSP;}        public int getindex ()  {            return index;       }        public void setindex (int index)  {            this.index = index;       }  }    usage Four: Methods of overriding enumerations

An example of a ToString () method overlay is given below.

  Java code   public enum color {       red ("Red",  1),  green ("green",  2),  blank ("white",  3),  yello ("Yellow",  4);        //  member variables        private String name;        private int index;       //  Construction Method         private color (string name, int index)  {            this.name = name;            this.index = index;       }        //covering method         @Override        public  string tostring ()  {           return  this.index+ "_" +this.name;       }  }     Usage Five: Implement Interface

All enumerations inherit from the Java.lang.Enum class. Because Java does not support multiple inheritance, enumeration objects can no longer inherit other classes.

  Java code   public interface behaviour {       void  Print ();       string getinfo ();  }   public enum  Color implements Behaviour{       red ("Red",  1),  green (" Green ",  2),  blank (" white ",  3),  yello (" Yellow ",  4);       //  Member variables        private String name;        private int index;       //  Construction Methods         private color (string name, int index)  {            this.name = name;            this.index = index;       }  /interface method         @Override        public string getinfo ()  {            return this.name;       }       //interface Method         @Override         public void print ()  {            System.out.println (this.index+ ":" +this.name);       }  }    Usage Six: Organize enumerations using interfaces

Java code public interface Food {enum Coffee implements food{Black_coffee,decaf_coffee,latte,cappuccino Enum dessert implements food{FRUIT, CAKE, GELATO}} Usage VII: About enumeration Collection use

Java.util.EnumSet and Java.util.EnumMap are two enumerated collections. Enumset guarantees that the elements in the collection are not duplicates; The key in Enummap is the enum type, and value can be any type. The use of this two collection is not here to repeat, you can refer to the JDK documentation.

For the implementation details and principles of enumerations, please refer to:

Reference: "Thinkinginjava" fourth edition


http://softbeta.iteye.com/blog/1185573

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.