Java enumeration using a detailed, well organized, can only accept ...

Source: Internet
Author: User

Java enumeration using detailed

In actual programming, there are often such "datasets", whose values are stable in the program, and the elements in the "dataset" are limited.

For example, from Monday to Sunday, seven data elements formed the "data set" of the week, with four data elements forming the "datasets" of the Four seasons.

How do you better use these "datasets" in Java? So the enumeration comes in handy, and the following code details the use of enumerations.

Package com.ljq.test;

/**
* Explanation of enumeration usage
*
* @author Jiqinlin
*
*/
public class Testenum {
/**
* Normal Enumeration
*
* @author Jiqinlin
*
*/
public enum Colorenum {
Red, green, yellow, blue;
}

/**
* Enumerations can add properties and methods like normal classes, and you can add static and non-static properties or methods to it
*
* @author Jiqinlin
*
*/
public enum Seasonenum {
Note: The enumeration is written first, otherwise the compilation error
Spring, summer, autumn, winter;

Private final static String position = "Test";

public static Seasonenum Getseason () {
if ("Test". Equals (position))
return spring;
Else
return winter;
}
}

/**
* Gender
*
* Implementation of enumerations with constructors
*
* @author Jiqinlin
*
*/
public enum gender{
Assign a value through parentheses, and must have a parameter constructor and a property and method, or compile an error
Assignments must either be assigned or not assigned, and cannot be assigned a part of the value, or the constructor cannot be written if no value is assigned, and the assignment compiles with errors
Man (the "Man"), Women ("women");

Private final String value;

Constructors can only be private by default, ensuring that constructors are only used internally
Gender (String value) {
This.value = value;
}

Public String GetValue () {
return value;
}
}

/**
* Order Status
*
* Implementation of enumerations with abstract methods
*
* @author Jiqinlin
*
*/
public enum Orderstate {
/** Canceled */
Cancel {public String GetName () {return ' canceled ';}},
/** Pending Review * *
waitconfirm {public String getName () {return ' Pending review;}},
/** Waiting for payment * *
waitpayment {public String getName () {return "Waiting for Payment";}},
/** is in stock * *
admeasureproduct {public String getName () {return ' is in stock ';}},
/** Waiting for delivery * *
waitdeliver {public String getName () {return "Awaiting shipment";},
/** shipped * *
Delivered {public String getName () {return ' Shipped ';}},
/** Goods received */
RECEIVED {public String getName () {return ' receipt ';}};

Public abstract String getName ();
}

public static void Main (string[] args) {
An enumeration is a type that defines a variable to limit the assignment of a variable, and a value in an enumeration that is obtained through the enumeration name. Value when assigned.
Colorenum colorenum = Colorenum.blue;
Switch (colorenum) {
Case RED:
SYSTEM.OUT.PRINTLN ("Color is red");
Break
Case GREEN:
SYSTEM.OUT.PRINTLN ("Color is green");
Break
Case YELLOW:
SYSTEM.OUT.PRINTLN ("Color is yellow");
Break
Case Blue:
SYSTEM.OUT.PRINTLN ("Color is blue");
Break
}

Traversal enumeration
System.out.println ("traverse the values in the Colorenum enumeration");
For (Colorenum color:ColorEnum.values ()) {
SYSTEM.OUT.PRINTLN (color);
}

Gets the number of enumerations
System.out.println ("The value in the Colorenum enumeration has" +colorenum.values (). length+ "a");

Gets the index position of the enumeration, starting from 0 by default
System.out.println (ColorEnum.red.ordinal ());//0
System.out.println (ColorEnum.green.ordinal ());//1
System.out.println (ColorEnum.yellow.ordinal ());//2
System.out.println (ColorEnum.blue.ordinal ());//3

Enumeration implements the Java.lang.Comparable interface by default
System.out.println (ColorEnum.red.compareTo (Colorenum.green));//-1

//--------------------------
System.out.println ("===========");
System.err.println ("season for" + Seasonenum.getseason ());


//--------------
System.out.println ("===========");
For (Gender gender:Gender.values ()) {
System.out.println (Gender.value);
}

//--------------
System.out.println ("===========");
For (Orderstate order:OrderState.values ()) {
System.out.println (Order.getname ());
}
}

}
Category: Java Advanced features

Java enumeration using a detailed, well organized, can only accept ...

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.