I genius official Free Tutorial 25: Enumeration enum of basic Java Tutorials

Source: Internet
Author: User
Tags naming convention

Enumeration

The enumeration is simply to enumerate the required constant values one by one and encapsulate them as a whole.
Syntax format:[public] enum enum name {//value list}
Declaration format: enumeration name variable name;
Assignment format: variable name = enumeration name. Value
Each of the values enumerated is subject to the naming rules of the identifiers and cannot be written arbitrarily

Instance:package enumdemo.simple;/** *  creating an enumeration type week  Note: The enumeration is created here, that is, the data type is an enum, not a class  * @ author  Genius Federation  -  Yukun  */public enum Week {//  lists the values of the week, separated by commas in English format//  There is no type in the enumeration, just list the required values Monday,  Tuesday,  Wednesday,  Thursday,  Friday,  Saturday,  Sunday}package _enum.simple ;/** *  use of the test enumeration  *  @author   Genius Federation  -  Yukun  */public class Test  {Public static void main (String[] args)  {//  application in switch, so week represents Wednesday week  Week = week. Wednesday;//  the variable weekswitch  (week) of the enumeration type is passed in here, and the case value here is directly using the value that exists in the enumeration. Cannot use other values case  Monday: System.out.println ("Monday");break; case  Tuesday: System.out.println ("Tuesday"); break;case   Wednesday: System.out.println ("Wednesday");break;case  Thursday: System.out.println ("Thursday");break;case  Friday: System.out.println ("Friday");break;case  Saturday: System.out.println ("Saturday");break;case  Sunday: System.out.println (" Sunday "); break;}} Output Result: Wednesday


The following is a beginner's reading

public  enum  week{//these values are converted to the Week type of the attribute (variable) name Monday,  Tuesday,  Wednesday,  Thursday,  Friday,   Saturday,  Sunday}//The following code is an enumeration week converted into classes after each value enumerated in the code//enumeration to conform to the identifier naming convention, because the enumeration is of reference type, when the underlying processing, the enumeration week is converted to public   final  class  Week  extends java.lang.Enum<Week>{public   static  final  Week   Monday;  public  static  final   Week   Tuesday; public  static  final  week   Wednesday ; public  static  final  week   Thursday;  public  static   final  Week   Friday; public  static  final   week   Saturday; public  static  final  week   Sunday; public   static  final  Week[]   $VALUES; static{Monday  = new   week (); Tuesday  = neW  week (); Wednesday  = new  week (); Thursday  = new  week (); Friday  =  new  week (); Saturday  = new  week (); Week VII  = new  week (); $ values = {Monday,  Tuesday,  Wednesday,  Thursday,  Friday,  Saturday,  Week VII}}public  static   week[]  values () {return   (week[]) ($VALUES.  clone ());} Public static week valueof (String  name) {return   (Week) (super. valueOf (Week.class, name));}}


You can see that all the members are static in the converted week class, so the attribute of the value type is raised
Note: Although enumerations are converted to classes at the bottom, You do not need to use new to create an object. Attributes that need to be passed are also value-passed

In addition, enumerations can have properties and methods, including construction methods;

The example 1:public enum Week{//mon is converted to a Week type property, where the Week object is created by calling Week's parameterless construct mon ()}public enum week{//because of the declaration of a constructor with a string type argument, So this is the case with parameters//NOTE: The Declaration of the property, method, and constructor must be placed after the enumeration value Mon ("Monday")//Declare a constructor with parameters in enumeration Week//Note: The enumeration declares that the constructor method can only be decorated with private or not written, Cannot use public and protectedprivate Week (String value) {///enumeration can also use the This keyword This.value = value;} Properties and methods can use all the access modifiers to decorate private string value;//to declare a method to get value public string GetValue () {//no local variable and property value conflict, The keyword this can omit return value;}}    public class Test{public static void Main (string[] args) {//Output MON's value System.out.println (Week.MON.getValue ()); }} Output: Monday


Example 2:/** *  enumeration week *  @author   Genius Alliance  -  Yukun  */enum week {/* *   enumeration value  *  because the enumeration declares a construction method with an int type parameter   *  so the enumeration value needs to be enclosed in parentheses () with a value of type int  *  This value is passed to the parameter Value */    mon in the constructor method (1),  tue (2),  wed (3),  thu (4),  fri (5) ,      //sat (6) {} This write function is similar to anonymous inner class     sat (6)  {     //  overriding GetValue methods          @Override          public int getvalue ()  {             return 60;        }     },    sun (7)  {         @Override          public int getvalue ()  {             return 70;        }    };         //  Properties of Enumerations     private int value;     //  construction method for the enumeration     week (Int value)  {         this.value = value;    }    //   General method of the enumeration     public int getvalue ()  {         return value;    }}/** * enumtest class: Used to test enumeration  *  @author   Genius Alliance  -  Yukun  */public class enumtest {    public  static void main (String[] args)  {         System.out.println ("enumtest.fri   value = "  + week.fri.getvalue ());       &Nbsp; system.out.println ("enumtest.sat   value = "  + week.sat.getvalue ());     }} Run Results:enumtest.fri   value = 5EnumTest.SAT   value  = 60


This article from the "Genius Union Education Official Blog" blog, reproduced please contact the author!

I genius official Free Tutorial 25: Enumeration enum of basic Java Tutorials

Related Article

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.