Detailed introduction to the use of enumeration in Java

  • 2020-04-01 01:42:17
  • OfStack

Enumeration characteristics

1. Enum definition enum class inherits the java.lang.enum class by default instead of the Object class. The Java. Lang. Enum class implements the Java lang. Two interface Serializable and java.lang.Com parable

2. The constructor of enumeration class can only use private access modifier. If the access control character of its constructor is omitted, private modifier is used by default.

3. All instances of an enumeration class must be listed explicitly in the enumeration class, otherwise the enumeration class will never be able to generate an instance. When these instances are listed, the system automatically adds a public static final modifier without the programmer explicitly adding it.


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 an enumeration

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 the enumeration instance

Int ordinal() returns the index of the enumeration value in the enumeration

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

Public static the valueOf ()


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
    }
}

The constructor of an enumeration

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;
    }

}

Example of comprehensive application of enumeration: traffic lights

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201304/201304270921482.png ">


public enum Lamp {
         
     S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),
     
     N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),
     
     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;
     }

 
         
     private boolean lighted;
         
     private String opposite;
         
     private String next;
     public boolean isLighted(){
         return lighted;
     }

         
     public void light(){
         this.lighted = true;
         if(opposite != null){
             Lamp.valueOf(opposite).light();
         }
         System.out.println(name() + " lamp is green There should be 6 Cars can be seen passing in all directions! ");

     }

         
     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(" A green light from " + name() + "--------> Switch to a " + next);            
             nextLamp.light();
         }
         return nextLamp;
     }
 }


Related articles: