Introduction to the detailed use of enumerations in Java _java

Source: Internet
Author: User

Enumeration features

1. Defining an enumeration class with an enum inherits the Java.lang.Enum class by default, rather than inheriting the object class. Where the Java.lang.Enum class implements the Java.lang.Serializable and java.lang.Comparable two interfaces

2. The constructor of an enumeration class can only use the private access modifier and, if the access control character of its constructor is omitted, the private adornment is used by default;

3. Enumerating all instances of a class must be explicitly listed in the enumeration class, otherwise this enumeration class will never produce an instance. When these instances are listed, the system automatically adds the public static final decoration without the programmer being explicitly added.

Copy Code code as follows:

public enum Week {
mon{
Public String tolocalestring () {
return "Monday";
}
},tues{
Public String tolocalestring () {
return "Tuesday";
}
},web{
Public String tolocalestring () {
return "Wednesday";
}

},thur{
Public String tolocalestring () {
return "Thursday";
}

},fri{
Public String tolocalestring () {
return "Friday";
}

},sat{
Public String tolocalestring () {
return "Saturday";
}

},sun{
Public String tolocalestring () {
return "Sunday";
}

};
Public abstract String tolocalestring ();
}

Traversal of enumerations
Copy Code code as follows:

public class Enumtest {
public static void Main (string[] args) {
For (Week w:week.values ()) {
System.out.println (w);
}
}
}

common methods of enumeration

int CompareTo method

String name () returns the name of an enumeration instance

int ordinal () returns the index of an enumeration value in an enumeration

String toString () returns the instance name of the enumeration is more common than name

public static valueof ()

Copy Code code as follows:

public class Enumtest {
public static void Main (string[] args) {
Week Day =week.fri;
System.out.println (day);//fri
System.out.println (Day.name ());//fri
System.out.println (Day.ordinal ());//4
System.out.println (week.valueof ("SUN"). toLocaleString ());//Sunday
System.out.println (Week.values (). length);//7 Get enumeration length
}
}

Constructors for enumerations
Copy Code code as follows:

public enum Gender {
MALE ("male"), FEMALE ("female");
private String name;
Private Gender (String name) {
THIS.name =name;
}
Public String GetName () {
return this.name;
}
Public String toString () {
String name = NULL;
Switch (this) {
Case MALE:
Name= "Male";
Break
Case FEMALE:
Name= "female";
Break
}
return name;
}

}

examples of integrated applications of enumerations: traffic lights

Copy Code code as follows:

public enum Lamp {
/* Each enumeration element represents a control light in one direction.
S2n ("N2s", "s2w", false), s2w ("n2e", "e2w", false), e2w ("w2e", "E2s", false), E2s ("w2n", "s2n", false),
/* The following elements represent the lights in the opposite direction of the above elements, and their "opposite direction light" and "next light" should be ignored! */
N2s (Null,null,false), n2e (Null,null,false), w2e (Null,null,false), w2n (Null,null,false),
* * from south to east and from west to north, such as the right corner of the light is not controlled by the traffic lights, so you can assume that they always green
S2E (Null,null,true), e2n (Null,null,true), n2w (Null,null,true), W2s (null,null,true);

Private Lamp (String opposite,string Next,boolean lighted) {
This.opposite = opposite;
This.next = Next;
this.lighted = lighted;
}


/* The current light is green * *
private Boolean lighted;
/* The corresponding direction of green with the current light * *
Private String opposite;
/* The current lights turn red now a turn green lamp * *
Private String Next;
public Boolean islighted () {
return lighted;
}

/**
* When a lamp turns green, its corresponding direction of the light will also turn green
*/
public void Light () {
This.lighted = true;
if (opposite!= null) {
Lamp.valueof (opposite). Light ();
}
SYSTEM.OUT.PRINTLN (name () + "lamp is green, there should be a total of 6 directions below to see the car go through!" ");

}

/**
* When a lamp turns red, the corresponding direction of the lamp will also turn red, and the next direction of the light to turn green
* @return The next light to turn green
*/
Public Lamp Blackout () {
this.lighted = false;
if (opposite!= null) {
Lamp.valueof (opposite). Blackout ();
}

Lamp nextlamp= null;
if (next!= null) {
Nextlamp = lamp.valueof (next);
System.out.println ("green light from" + name () + "--------> Switch to" + next);
Nextlamp.light ();
}
return nextlamp;
}
}

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.