1. A simple seasonal enumeration
1. Seasonal enumeration class, no longer inheriting other classes public
enum Seasonenum {
spring,summer,fall,winter;
}
//2.main test
Public Class Testenums {public
static void Main (String [] a) {
//the ToString () square method is invoked when the output seasonenum.spring is printed. Enumeration By default overrides the ToString () method, returning the constant name (name)
System.out.println (seasonenum.spring) directly
;
The output is: SPRING
the low-level implementation of the 2.SeasonEnum seasonal enumeration
Seasonenum inherits the enum by default, so seasonenum can no longer inherit other classes public final class Seasonenum extends enum {//1. Enumeration objects
public static final Seasonenum SPRING;
public static final Seasonenum SUMMER;
public static final Seasonenum FALL;
public static final Seasonenum WINTER;
private static final Seasonenum enum$values[];
3. Privatisation constructor, so the enumeration object defaults to be unable to get the private seasonenum (String s, int i) {super (S, i);
public static seasonenum[] values () {seasonenum aseasonenum[];
int i;
Seasonenum aseasonenum1[];
System.arraycopy (aseasonenum = enum$values, 0, aseasonenum1 = new seasonenum[i = Aseasonenum.length], 0, I);
return ASEASONENUM1; //4. Can get the enumeration public static Seasonenum valueof (String s) {return (Seasonenum) ENUM.V by S
Alueof (Com/xmg/enums/seasonenum, s); //2. Instantiating an Enumeration object STATIC {spring = new Seasonenum ("Spring", 0);//The first argument is a constant name, and the second argument is: The index of the constant SUMMER = new Seasone
Num ("SUMMER", 1);
FALL = new Seasonenum ("FALL", 2);
WINTER = new Seasonenum ("WINTER", 3);
Enum$values = (new seasonenum[] {SPRING, SUMMER, FALL, WINTER}); }
}
Summarize:
The constants defined in the enumeration, referencing an enumerated object that is instantiated by the default privatisation constructor for a simple use of the 3.SeasonEnum seasonal enumeration
public class Testenums {public
static void Main (String [] a) {
System.out.println (seasonenum.spring);/ The output is: SPRING
seasonenum summer = seasonenum.summer;//Instantiate an enumerated object
//switch The underlying comparison is the index
switch (summer) that contains the constants. /JDK1.5 's switch supports enumeration comparisons: Incoming enumeration object case
summer://constant
System.out.println (SUMMER); the output is: SUMMER break
;
Default: Break
;
}
If the underlying comparison is an enumeration reference
if (Summer==seasonenum.summer) {//Is true
System.out.println (summer);//The output result is: Summer
}
}
}
The result of performing main output is:
SPRING
SUMMER
SUMMER
The principle of the underlying implementation
public class Testenums
{public
static void Main (String a[])
{
System.out.println (seasonenum.spring) ;
Seasonenum summer = Seasonenum.summer;
The Summer.ordinal () method is to get the summer illuminated steady index in an enumerated array, the return value is 2
switch ($SWITCH _table$com$xmg$enums$seasonenum () [ Summer.ordinal ()])
{case
2://' \002 '
System.out.println (summer);
break;
if (summer = = Seasonenum.summer)
System.out.println (summer);
}
.....
.....
.....
.....
}
Summarize
Enumeration, in fact, compares the index and the reference , not the constant's name 4. The constants of the enumeration can also be used in Chinese
Enum class public
enum SeasonEnum2 {
Spring, summer, autumn, winter;
}
Test class public
class TestEnums2 {public
static void Main (String [] a) {
System.out.println (SeasonEnum2. Spring); /Output The result is: Spring
SeasonEnum2 summer = SeasonEnum2. Summer///Instantiate an enumerated object
//switch The underlying comparison is the index
switch (summer) that contains the constants. jdk1.5 switch supports enumeration comparisons: Incoming enumeration object Case
Summer://enum constant
System.out.println (summer); The result is: Summer break
;
Default: Break
;
}
If the underlying comparison is an enumeration reference
if (summer==seasonenum2. Summer) {
System.out.println (summer);//output result: Summer
}
}
}
Results of output:
Spring
Summer and summer
Summarize
The comparison of enumerations, in fact, is the index and reference , not the name of the constant 5. Custom builder, which is worth comparing
Customize an enumeration public
enum Diskcacheenum {//constant name that uses the hard disk cache to
pass arguments to the constructor when enumerating objects)
true (True), False (false);
Custom boolean field
private Final Boolean isdiskcache;
Custom constructors, which are private by default (the constructor is called when Diskcacheenum.true is invoked)
Diskcacheenum (Boolean isdiskcache) {
this.isdiskcache= Isdiskcache;
}
Gets the value of the field public
Boolean Isdiskcache () {return
isdiskcache;
}
}
Test (Compare the value of the field instead of comparing the references and indexes of the enumeration) public
static void Main (String [] a) {
diskcacheenum diskcacheenum = Diskcacheenum.true;
System.out.println (diskcacheenum);//output: True
if (Diskcacheenum.isdiskcache ()) {//To determine whether the value of the field of a Boolean value is True
System.out.println (Diskcacheenum.isdiskcache ());//Output Boolean value: True
}
}
Summarize
This enables you to compare the values obtained from the database by creating a new enumeration (using the values of the enumeration) 6. Designing a single example with enumerations
a hungry man-type single example:
Class SingleTon {
private static SingleTon instance=new SingleTon ();
Private SingleTon () {} public
static Singledemo getinstance () {return
instance;
}
}
Acquisition of a single Case object:
SingleTon instance = singleton.getinstance ();
implement a Hungry man by enumeration:
public enum SingleTon {
INSTANCE
}
Acquisition of a single Case object:
SingleTon instance = singleton.instance;