Java SE learning enumeration Summary

Source: Internet
Author: User

Java SE learning enumeration Summary

This article is a summary of the articles on the Internet and some of my own practices. Thank you for your selfless sharing.

I recently encountered an enumeration introduction when I was reading books on Java basics. I checked some information online and summarized the information.

Usage 1: Constant

Package meiju;/***** @ ClassName: Color * @ Description: constant usage of enumeration variables * @ author zhangjunshuai junshuaizhang@163.com * @ date 07:29:40 */public enum Color {RED, GREEN, BLANK, YELLOW}

Usage 2: switch

Package meiju;/***** @ ClassName: Signal * @ Description: enumeration switch usage * @ author zhangjunshuai junshuaizhang@163.com * @ date 07:33:32 */enum Signal {GREEN, YELLOW, RED} public class EnumForswitch {Signal color = Signal. RED; public void change () {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 3: Methods in Enumeration

Package meiju;/***** @ ClassName: ColorMthod * @ Description: enumeration Method * @ author zhangjunshuai junshuaizhang@163.com * @ date 2014-11-16 PM 07:35:09 **/enum ColorMthod {RED ("RED", 1), GREEN ("GREEN", 2 ), BLANK ("white", 3), YELLOW ("YELLOW", 4); private String name; private int index; // What is the construction method of enumeration? Private ColorMthod (String name, int index) {this. name = name; this. index = index;} // common 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 4: overwrite the enumeration method

Package meiju;/***** @ ClassName: EnumMethodOverride * @ Description: override Method * @ author zhangjunshuai junshuaizhang@163.com * @ date 07:37:04 */public class EnumMethodOverride {public enum ColorMethod {RED ("RED", 1), GREEN ("GREEN ", 2), BLANK ("white", 3), YELLO ("yellow", 4); private 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 (ColorMethod. RED );}}

Usage 5: Implementation Interface

Package meiju;/***** @ ClassName: Behavior * @ Description: enumeration implementation interface * @ author zhangjunshuai junshuaizhang@163.com * @ date 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); private 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 stubColorInterface. RED. print ();}}

Usage 6: Use of enumeration Sets

Package meiju; import java. util. enumMap; import java. util. enumSet;/**** @ ClassName: EnumLIghtTest * @ Description: enumeration traversal * @ author zhangjunshuai junshuaizhang@163.com * @ date 07:41:13 */public class EnumLIghtTest {/1. defines the enumerated type public enum Light {// 2. use the construction parameters to pass the parameter RED (1), GREEN (3), YELLOW (2); // defines the private int nCode; // constructor, the enumeration type can only be private Light (int _ nCode) {this. nCode = _ nCode;} public String toSt Ring () {return String. valueOf (this. nCode) ;}}/*** @ param args */public static void main (String [] args) {// 1. traverse the enumeration type System. out. println ("demonstrate Enumeration type traversal ...... "); TestTranversalEnum (); // 2. Use System. out. println (" to demonstrate the use and traversal of EnumMap objects ...... "); TestEnumMap (); // 3. demonstrate how to use System. out. println (" to demonstrate how to use and traverse EnumSet objects ...... "); TestEnumSet ();}/*** demonstrate enumeration 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) ;}/ *** demonstrate the use of EnumMap. The use of EnumMao is similar to that of HashMap, only the key value is of the enumeration type */private static void testEnumMap () {// 1. this example shows how to define an EnumMap object. The constructor of an EnumMap object requires parameter input. The default value is EnumMap of the key type.
 
  
CurrEnumMap = new EnumMap
  
   
(Light. class); currEnumMap. put (Light. RED, "RED Light"); currEnumMap. put (Light. GREEN, "GREEN light"); currEnumMap. put (Light. YELLOW, "YELLOW Light"); for (Light aLight: Light. values () {System. out. println ("[key =" + aLight. name () + ", value =" + currEnumMap. get (aLight) + "]") ;}}/*** demonstrate how to use EnumSet. EnumSet is an abstract class, you can use the allOf Method */private static void testEnumSet () {EnumSet to obtain the content of an enumeration type.
   
    
CurrEnumSet = EnumSet. allOf (Light. class); for (Light aLightSetElement: currEnumSet) {System. out. println ("data in the current EnumSet is:" + aLightSetElement );}}}
   
  
 

Usage 7: organize enumeration Using Interfaces

public interface Food {enum Coffee implements Food{BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO}enum Dessert implements Food{FRUIT, CAKE, GELATO}}

Enumeration, compared with final static, can reduce the definition of variables. The use of enumeration in the switch is conducive to code execution efficiency. At the same time, enumeration can customize built-in methods, and enumeration can use traversal methods. Although enumeration has some advantages, enumeration is not inherited.

Refer:

Http://www.iteye.com/topic/1116193

Http://blog.csdn.net/p106786860/article/details/11516269

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.