Analysis of the meanings of enum-type enums

Source: Internet
Author: User

An enumeration type is a basic data type, not a constructed data type, but a new type from jdk1.5, which is generally used to make global constants .
Characteristics:

1. It cannot have a public constructor This ensures that the client code has no way of creating an instance of an enum that can be used to write a singleton pattern.
2. All < Span style= "COLOR: #ff0000" The enumeration value is public, static, final 3. The enum contains the ToString method, so we call Color.Blue.toString () to return the string "Blue" by default.  


Usage:1: string constant (also the most concise singleton mode):
Define public enum  enumkey{    Message_update,message_delete,message_add;} Use EnumKey.MESSAGE_ADD.toString ();

2: Integral type constant
Define public enum EnumKey {    message_update (1), Message_delete (2), Message_add (3);    public int tag;    EnumKey (int iTag) {        this.tag = ITag;    }} use int i =enumkey.message_add.tag;

3: string constant (the second method of presentation)
Define public enum EnumKey {    message_update ("UPDATE"), Message_delete ("DELETE"), Message_add ("ADD");    Public String msg;    EnumKey (String str) {        this.msg = str;    }} Use string s =enumkey.message_add.msg;

4: Add method
Define public enum EnumKey {    message_update ("UPDATE", 1), Message_delete ("DELETE", 2), Message_add ("ADD", 3);    Public String msg;    public int tag;    EnumKey (String str, int iTag) {        this.msg = str;        This.tag = ITag;    }    public static Boolean getmax (int flag) {        return flag > 1? true:false;    }} Use EnumKey.MESSAGE_ADD.getMax (2);

Summary:

'
Did you find anything yet? Each instance of the enumeration contains parameters that are consistent with the parameters contained by the enumeration's constructor method So you will find that in fact message_update ("UPDATE", 1) represents the meaning of EnumKey message_update=new EnumKey ("Update", 1), i.e.:
Public  class EnumKey {public    static String msg;    public static int tag;    Public EnumKey (String str, int iTag) {        this.msg = str;        This.tag = ITag;    }} EnumKey message_update=new EnumKey ("UPDATE", 1);





Analysis of the meanings of enum-type enums

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.