About Java enumerations

Source: Internet
Author: User

First, enumeration definitions
Enumeration cannot be defined in a static method
Generic custom enum types are subtypes of enum Enumtype
It has two static methods (class methods) by default: Values () and valueof ()
VALUES () returns an array of instances of type Enumtype
ValueOf () Returns an instance of the Enumtype type
Two
Example: Custom color enum type
color{White, BLACK, GRAY}
Color C = Color.gray;
So:
Color.valueof ("BLACK") = = Color.Black = = Color.values () [1]
Because they all return a color type
c.tostring () = = C.name ()
Because they are returning a string
Iii. constructors and member variables for enumerations
The constructor for an enumeration can only be private
The member variable of the enumeration is not restricted, but if the method defined in the enumeration instance needs access to the member variable in the enumeration type, the member variable is at least protected, otherwise it cannot inherit

Package newpack17;interface d{public String getdescription (); public int Geti ();}        Public enum Anyenum implements d{constants_a{public String getdescription () {return "I am enum member a";        } public int Geti () {return i;        }}, constants_b{public String getdescription () {return ' I am enum member B ';        } public int Geti () {return i;        }}, Constants_e ("enumeration member E", "n") {public String getdescription () {return this.str;        } public int Geti () {return this.it;        }}, Constants_c, constants_d{public String getdescription () {return "I am enum member D";        } public int Geti () {return i;    }    };        Private Anyenum (String str,int i) {this.str = str;    this.it=i;    } private Anyenum () {} public String getdescription () {return "I am enum member xxx"; } public int Geti () {return 10000; } public int it;    Protected is OK, but cannot be private protected String str; private static int i = 5; Static member public protected,private all lines public static void main (string[] args) {for (int i = 0; I<anyenum.values () . length;            i++) {System.out.println (Anyenum.values () [i]+] calls the GetDescription () method as: "+anyenum.values () [I].getdescription ()];        The System.out.println (Anyenum.values () [i]+ "Call Geti () method is:" +anyenum.values () [I].geti ()]; }    }}

About Java enumerations

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.