Java enumeration (enum) detailed 7 common usage

Source: Internet
Author: User

JDK1.5 introduces a new type--enumeration. In Java it is a "small" function, but to my development has brought "big" convenience.

Big Brother I add my own understanding, to help you understand. 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 the  enum  instance.   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)  {      &NBsp;    for  (Color c : color.values ())  {                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;       }        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

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.