Fixed and limited number of objects, it may be enumerated
The method of construction of a private nature, coupled with public static final properties;
Data type: enum Enum default inheritance
So public enum extends behind EnumTest not inherit with other classes
Attributes:
Enumeration object name name
Enum ordinal position of the object, similar to the index
Common methods:
valueOf () Gets an enumeration of the object by the given name enumeration
All values obtained enumeration object and returns an array
compareTo () compares two objects Enumeration
toString no final modifications can be rewritten.
Enumeration can also modify their own attributes and methods
1. In the first row must be the same format described enumerate the contents, and the constructor
2. Constructors must be private
3. Other general and basically the same class.
Package EnumTest; public enum EnumTest { // members were EnumTest variable type, name () The default is the Sunday, catalog on Monday, on Tuesday, catalog on Wednesday, catalog on Thursday, catalog on Friday, Satday // must end with a semicolon in the first row description sunday ( "SundayName", 1,1) , Monday ( "MondayName", 22,2), Tuesday ( "TuesdayName", 33,3), Wednesday ( "WednesdayName", 44,4), Thursday ( "ThursdayName", 55,5), catalog on Friday ( "FridayName", 66,6 ), Satday; public String name; // the Enum itself with the name attribute, Private Final String name; Private int index; public int Age; public String getName () { return the this .name; } privateEnumTest () { // This is the default constructor, do not add the words, enumerate the contents of the first line is the name must be (name, age, index) format, the addition of this Satday so we can directly write } Private EnumTest (String name, int Age, int index) { // constructor must enumeration private, the method may overload the this .name = name; the this .age = Age; the this .index = index; } }
Package EnumTest; public class EnumTestMain { public static void main (String [] args) { EnumTest Sun = EnumTest.Sunday; System.out.println ( "Sun =" + Sun); EnumTest Mon = EnumTest.valueOf ( "catalog on Monday") ; // valueOf corresponding object may return the enumerated object name System.out.println ( "mon.age =" + mon.age); // custom property Age System.out.println ( "Mon =" mon.name + ()); // the Enum own method returns the object name System.out.println ( "Mon =" + mon.getName ()); //Custom method, return a custom attribute name, and Enum no conflict, although their definition of the name and the name Enum same name, but did not affect each other mon.name = "Monday after the modified name" ; System. Out.println ( "Mon =" + mon.name ()); // the Enum own method returns the object name System.out.println ( "Mon =" + mon.getName ()); // custom methods, returns the custom property name System.out.println ( "-------------------------------"); // values the enum content into the array returned EnumTest [] = eArr EnumTest.values (); for (E EnumTest: eArr) { System.out.println (e.name ()); } System.out.println ( "---- --------------------------- ");// enumeration can switch judgment switch(sun){ case Sunday: System.out.println("swtch 判断 sun = Sunday"); break; case Monday: System.out.println("swtch 判断 sun = Monday"); break; case Tuesday: System.out.println("swtch 判断 sun = Tuesday"); break; case Wednesday: System.out.println("swtch 判断 sun = Wednesday"); break; case Thursday: System.out.println("swtch 判断 sun = Thursday"); break; case Friday: System.out.println("swtch 判断 sun = Friday"); break; case Satday: System.out.println("swtch 判断 sun = Satday"); break; } } }