Enumeration is a new reference data type and class in JDK1.5. The interface is at the same level. The key that defines enumeration is enum.
The java.lang.Enum class, which is the parent of all enumerations.
The essence of enumeration is multiple objects of a class.
Each member of an enumeration is an object of the class
public enum Color {
BLUE,GREEN,RED
}
public class EnumTest {
public static void main(String[] args) { System.out.println(Color.RED.getClass()); }}Copy the code
class kehao.enu.Color
Copy the code
Definition of enumeration
- Format:
publicEnmu enumerator {}Copy the code
-
Enumeration constants define:
- Constants in enumerations are capitalized, multiple constants are separated by commas, and the last constant may or may not be written with a semicolon. Each constant represents an object of that class. The modifier for
public static final
. - The enumeration has a default constructor for a private modifier with no arguments. If the constructor is written by hand, it must also be private. And the constructor must be written after the constant, so the last constant must be semicolon.
- Constants in enumerations are capitalized, multiple constants are separated by commas, and the last constant may or may not be written with a semicolon. Each constant represents an object of that class. The modifier for
public enum Color {
// Static constants for enumeration
RED,GREEN,YELLOW
}
Copy the code
public enum Color{
// Enumerates static constants directly to the color variable
RED("Red"),GREEN("Green"),YELLOW("Yellow");
private String color;
private Color(String color){
this.color = color ;
}
Get/set / / ignore
}
Copy the code
Use enumerations in switch statements
enum Signal {
GREEN, YELLOW, RED
}
public class TrafficLight {
Signal color = Signal.RED;
public void change(a) {
switch (color) {
case RED:
color = Signal.GREEN;
break;
case YELLOW:
color = Signal.RED;
break;
case GREEN:
color = Signal.YELLOW;
break; }}}Copy the code
Add a new method to the enumeration
If you want to customize your own methods, 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 Color {
RED("Red".1), GREEN("Green".2), BLANK("White".3), YELLO("Yellow".4);
// Member variables
private String name;
private int index;
// constructor
private Color(String name, int index) {
this.name = name;
this.index = index;
}
// Common method
public static String getName(int index) {
for (Color c : Color.values()) {
if (c.getIndex() == index) {
returnc.name; }}return null;
}
// get set method
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex(a) {
return index;
}
public void setIndex(int index) {
this.index = index; }}Copy the code
Override enumeration methods
Here is an example of overriding the toString() method.
public enum Color {
RED("Red".1), GREEN("Green".2), BLANK("White".3), YELLO("Yellow".4);
// Member variables
private String name;
private int index;
// constructor
private Color(String name, int index) {
this.name = name;
this.index = index;
}
// Override method
@Override
public String toString(a) {
return this.index+"_"+this.name; }}Copy the code
Implementing an interface
All enumerations inherit from the java.lang.Enum class. Because Java does not support multiple inheritance, enumeration objects can no longer inherit from other classes.
public interface Behaviour {
void print(a);
String getInfo(a);
}
public enum Color implements Behaviour{
RED("Red".1), GREEN("Green".2), BLANK("White".3), YELLO("Yellow".4);
// Member variables
private String name;
private int index;
// constructor
private Color(String name, int index) {
this.name = name;
this.index = index;
}
// Interface method
@Override
public String getInfo(a) {
return this.name;
}
// Interface method
@Override
public void print(a) {
System.out.println(this.index+":"+this.name); }}Copy the code
Use interfaces to organize enumerations
public interface Food {
enum Coffee implements Food{
BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO
}
enum Dessert implements Food{
FRUIT, CAKE, GELATO
}
}
Copy the code
Note: We can use == to compare values between objects of enumerated types. We do not have to use the equals method
www.cnblogs.com/singlecodew…