Java Enum Application Summary

Source: Internet
Author: User

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.

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. 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.

public enum Code {SUCCESS (10000, "Operation succeeded"), FAIL (10001, "Operation failed");
	private int code;
	
	Private String msg;
		Private code (int code, String msg) {this.code = code;
	this.msg = msg;
	public int GetCode () {return code;
	public void Setcode (int code) {This.code = code;
	Public String getmsg () {return msg;
	public void Setmsg (String msg) {this.msg = msg; /** * To obtain code * @param value * @return/public static code getbyvalue (int value) (Code
            Code:values ()) {if (code.getcode () = = value) {return code;
	} return null; }

}
public class Codetest {public
	
	static void Main (string[] args) {
		int code = Code.SUCCESS.getCode ();
		Switch (Code.getbyvalue (Code)) {case
		SUCCESS:
			System.out.println ("OK ...");
			break;
		Case FAIL:
			System.out.println ("error ...");
			break;
		Default:
			System.out.println ("Default ...");
			break;
		}}}
Usage Three: 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.

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 variable  
    private String name;  
    private int index;  
    Construction Method  
    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 Four: Organize enumerations using interfaces
Public interface Food {  
    enum Coffee implements food{  
        Black_coffee,decaf_coffee,latte,cappuccino  
    }  
    enum Dessert implements food{  
        FRUIT, CAKE, GELATO  
    }  
Usage Five: About the use of enumeration collections
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.

Reference Blog: http://blog.csdn.net/qq_27093465/article/details/52180865

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.