java enum usage

Reprinted blog: https://www.cnblogs.com/liaojie970/p/6474733.html

java Enum principle 

public enum Size{ SMALL, MEDIUM, LARGE, EXTRA_LARGE };

In fact, this type of statement is the definition of a class that has just four instances, in this try not to construct the new object.

Therefore, when comparing two values ​​enumerated type, never need to call the equals method, and the direct use of "==" on it. (The equals () == method is used as the two are the same effect)

Java Enum type of grammatical structure and syntax of java classes although not the same, it should be said that the difference is relatively large. But after the compiler generates a class file. After the class file decompile can see that actually generates a class that inherits java.lang.Enum <E>.

  E.g:

Copy the code
public enum WeekDay { 
     Mon("Monday"), Tue("Tuesday"), Wed("Wednesday"), Thu("Thursday"), Fri( "Friday"), Sat("Saturday"), Sun("Sunday"); 
     private final String day; 
     private WeekDay(String day) { 
            this.day = day; 
     } 
    public static void printDay(int i){ 
       switch(i){ 
           case 1: System.out.println(WeekDay.Mon); break; 
           case 2: System.out.println(WeekDay.Tue);break; 
           case 3: System.out.println(WeekDay.Wed);break; 
            case 4: System.out.println(WeekDay.Thu);break; 
           case 5: System.out.println(WeekDay.Fri);break; 
           case 6: System.out.println(WeekDay.Sat);break; 
            case 7: System.out.println(WeekDay.Sun);break; 
           default:System.out.println("wrong number!"); 
         } 
     } 
    public String getDay() { 
        return day; 
     } 
}
Copy the code

After decompiling WeekDay (the javap WeekDay command) is obtained as follows (to remove the assembly code):

Copy the code
public final class WeekDay extends java.lang.Enum{ 
    public static final WeekDay Mon; 
    public static final WeekDay Tue; 
    public static final WeekDay Wed; 
    public static final WeekDay Thu; 
    public static final WeekDay Fri; 
    public static final WeekDay Sat; 
    public static final WeekDay Sun; 
    static {}; 
    public static void printDay(int); 
    public java.lang.String getDay(); 
    public static WeekDay[] values(); 
    public static WeekDay valueOf(java.lang.String); 
}
Copy the code

Use a: constant

Before JDK1.5, we define constants are: public static fianl ..... Well now, with the enumeration constants it can be related to a group enumeration type, the enumeration and provides more than constant method.

public enum Color {  
  RED, GREEN, BLANK, YELLOW  
}

Usage of Two: switch

Before JDK1.6 switch statement only supports int, char, enum type, enum, make our code more readable.

Copy the code
enum Signal {
        GREEN, YELLOW, RED
    }

    public class TrafficLight {
        Signal color = Signal.RED;

        public void change() {
            switch (color) {
            case RED:
                color = Signal.GREEN;
                break;
            case YELLOW:
                color = Signal.RED;
                break;
            case GREEN:
                color = Signal.YELLOW;
                break;
            }
        }
    }
Copy the code

Use three: add a new method to enumeration

If you plan to customize your own way, then finally add a semicolon to be serialized in the enum instance. Requirements must be defined and Java enum instance.

Copy the code
Color enum {public 
    RED ( "red", 1), GREEN ( "green", 2), BLANK ( "White", 3), YELLO ( "yellow", 4); 
    // member variable 
    Private String name; 
    Private int index; 

    // Constructors 
    Private Color (String name, int index) { 
        this.name = name; 
        this.index = index; 
    } 

    // conventional method 
    public static String getName (int index) { 
        for (C Color: Color.values ()) { 
        IF (c.getIndex () == index) { 
            return c.NAME; 
        } 
        } 
        return null; 
    } 

    // SET GET method 
    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;
    }
    }
Copy the code

Usage four: covering enumeration method

Here's an example of the method covered toString ().

Copy the code
the Test class {public 
    public enum Color { 
        RED ( "red", 1), GREEN ( "green", 2), BLANK ( "White", 3), YELLO ( "yellow", 4); 
        // member variable 
        private String name; 
        Private int index; 

        // Constructors 
        Private Color (String name, int index) { 
            this.name = name; 
            this.index = index; 
        } 

        // coating method 
        @Override 
        public String toString () { 
            return this.index + "_" + this.name; 
        } 
    } 

    public static void main (String [] args) { 
        System.out.println (Color.RED.toString ()); 
    } 
}
Copy the code

Usage five: implement an interface

All of the enumeration classes inherit from java.lang.Enum. Because Java does not support multiple inheritance, so enumerable object can not inherit from other classes.

Copy the code
public interface Behavior { 
    void Print (); 

    String getInfo (); 
    } 

    public enum Color the implements Behavior { 
    RED ( "red", 1), GREEN ( "green", 2), BLANK ( "White", 3), YELLO ( "yellow", 4); 
    // member variable 
    Private String name; 
    Private int index; 

    // Constructors 
    Private Color (String name, int index) { 
        this.name = name; 
        this.index = index; 
    } 

    // interface method 

    @Override 
    public String getInfo () { 
        return this.name; 
    } 

    // interface method 
    @Override 
    public void Print () { 
        System.out.println (this.index + ":"+ this.name); 
    } 
    }
Copy the code

Usage six: Use Interface organization enumeration 

Copy the code
public interface Food {
        enum Coffee implements Food {
            BLACK_COFFEE, DECAF_COFFEE, LATTE, CAPPUCCINO
        }

        enum Dessert implements Food {
            FRUIT, CAKE, GELATO
        }
    }
Copy the code

Usage seven: Use an enumeration on the set

java.util.EnumSet and java.util.EnumMap are two enumerations. EnumSet ensure the collection of elements is not repeated; EnumMap enum type is the key, but may be any type of value. About the use of the two sets will not go into here,

You can refer to the JDK documentation

 

Guess you like

Origin www.cnblogs.com/zmdd/p/11882803.html