Singleton design pattern:
1, private constructors; 2, creating class object present; 3, provide external access methods
import java.util.jar.Attributes.Name; public class Week { Private String name; public static Final Week MON = new new Week ( "Monday" ); public static Final Week TUE = new new Week ( "Tuesday" ); public static Final Week WES = new new Week ( "Wednesday" ) ; private Week(String name){ this.name = name; } public String getName() { return name; } }
The enumeration class is to have multiple instances of Enum
All of the enumeration classes are subclasses of Enum
Enumeration must be placed on the first line
Enumeration methods of the class: ORDINAL () Returns the number compareTo (Enum e) comparing the number name () Returns the name of this enum constant, it declared in its enum declaration. valueOf(class<T> type , String name) Mon week2 = week2.valueOf (week2. class , Mon) by obtaining enumeration byte code file values() Week arr[] = Week.values(); for (Week week : arr) { System.out.println(week); }
// class enumeration
public enum Week { MON, TUE, WED; // do not like to write so much trouble above enumerated item must be placed on the first line
}
public class demon1_enum { public static void main(String[] args) { My Week = Week.MON; switch (my) { case MON: System.out.println ( "Monday" ); BREAK ; Case TUE: System.out.println ( "Tuesday" ); BREAK ; } } }
import java.util.Iterator; public class demon2_Enum { // Method enumeration class public static void main (String [] args) { Week arr[] = Week.values(); for (Week week : arr) { System.out.println(week); System.out.println(week.getClass()); System.out.println(week.getClass().getName()); } } }