Java enumeration (enum) verbose usage

Source: Internet
Author: User

Usage One: Constants

in the JDK1.5 before, we define constants that are: Public static fianl .... . Now, with enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants.  

public enum Color {

RED, GREEN, BLANK, YELLOW

}

Methods that are common in enumerations:

Values (): The Values () method returns an array of enum instances, and the elements in the array strictly maintain their array in the Enum's real column, and the elements in the array strictly maintain their order in the enum declaration, so you can use the values () array in the loop.

ordinal The () method returns an int value, which is the order in which each enum instance is declared, starting at 0. You can use = = to compare enum columns, and the compiler will automatically provide you with the Equals () and Hashcode () methods. The Enum class implements the comparable interface, so it has a CompareTo () method. At the same time, it also implements the serializable interface.

name The () method returns the name of the enum instance declaration, which is the same as using the ToString () method.

valueOf () is a static method defined in the enum that returns the corresponding enum instance based on the given name, and throws an exception if no instance of the given name exists.

Java enumeration (enum) 7 common uses

JDK1.5 introduces a new type--enumeration. In Java, although it is a "small" function, but to my development has brought "big" convenience.

Big bro I add my own understanding, to help you understand.

Usage One: Constants

Before JDK1.5, we defined constants: public static fianl ..... Now, with enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants.

Java code
    1. Public enum Color {
    2. RED, GREEN, BLANK, YELLOW
    3. }

Usage Two: Switch

The switch statement before JDK1.6 only supports int,char,enum types, and using enumerations can make our code more readable. J (dk1.7 can use a string)

Package com.it.exenum;

Enum signal{

Green,yellow,red

}

public class TrafficLight {

Signal color=signal.red;

public void Change () {

switch (color) {

Case RED:

System.out.println ("Red");

Break

Case YELLOW:

System.out.println ("yellow");

Break

Case GREEN:

System.out.println ("green");

Break

Default:

System.out.println ("other colors");

}

}

}

Usage Three: Add a new method to the enumeration

If you intend to customize your own method, you must add a semicolon at the end of the enum instance sequence. And Java requires that an enum instance be defined first.


public enum Exceptioncode {
Not_found ("404", "url not FOUND"),
Null_exception ("201", "null EXCEPTION"),
Class_castnot ("303", "type conversion error");

Private String Code;
Private String Exceptiondesc;

Private Exceptioncode (String code,string exceptiondesc) {
This.code=code;
This.exceptiondesc=exceptiondesc;
}


Public String Getexceptiondesc () {
return EXCEPTIONDESC;
public void Setexceptiondesc (String exceptiondesc) {

This.exceptiondesc = Exceptiondesc;
}
}

Usage Four: Implement Interface

All enumerations are inherited from the Java.lang.Enum class. Because Java does not support multiple inheritance, enumeration objects can no longer inherit from other classes. Enum types inherit enum

Package com.it.exenum;

Public interface Behaviour {
void print ();

String GetInfo ();
}

Package com.it.exenum;

Public enum Color implements behaviour {

Red ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4);

private String name;
private int index;

Private Color (String name, int index) {
THIS.name = name;
This.index = index;
}

@Override
public void print () {
System.out.println (This.index + ":" + this.name);
}

@Override
Public String GetInfo () {
return this.name;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

public int GetIndex () {
return index;
}

public void Setindex (int index) {
This.index = index;
}

}

Five. Organizing enumerations using interfaces

Public interface Food {
Enum Coffee implements food{
Black_coffee,decaf_coffee,latte,cappuccino
}

Enum Dessert implements food{
FRUIT, CAKE, GELATO
}

public static void Main (string[] args) {
Coffee[] Sd=food.coffee.values ();
for (Coffee coffee:sd) {
System.err.println (Coffee.name ());
}
}

Java enumeration (enum) verbose usage

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.