Enumeration class (comprehensive network usage)

What is an enumeration class?
Personal understanding is that it
defines a specification that you can only take values ​​from some fixed values, and limits the range such as gender, four seasons can use enumeration classes, and it is type-safe, which means that you cannot pass in other variables such as int To an enumeration class An
enumeration class can be defined in a class or can be defined separately using the enum keyword
. The definition format of an enumeration class is:

enum 类名{
    //枚举值
}

Enumeration to pay attention to the details:
1. The enumeration class is also a special class.
2. The default modifier for enumeration values ​​is public static final.
3. The enumeration value is the type of the class to which the enumeration value belongs, and then the enumeration value points to the object of this class.
4. The default modifier of the constructor of the enumeration class is private.
5. Enumeration classes can define their own member variables and member functions.
6. The enumeration class can customize the constructor, but the modifier of the constructor must be private.
7. Enumeration classes can have abstract methods, but enumeration values ​​must implement abstract methods.
8. The enumeration value must be in the first statement of the enumeration class.

In general, an enumeration value is an instance of an enumeration class. It can be a single enumeration value or an enumeration value can be bound to a variable.

Enumeration usage 1: constant value

enum Sex{
    男,
}//男 女是Sex的实例 分隔符用逗号

How to iterate over an enumeration?

 for(Sex s : Sex.values()){   
    System.out.println(Sex.name);
    //name是枚举值的名字  如上那就是男和女
 }

Enumeration usage two: switch

//季节枚举类
enum Season{
spring,summer,autumn,winter;
}
public eclass Demo8 {

public static void main(String[] args) {
    Season season = Season.summer;
    switch(season){
        case spring:
            System.out.println("春天...");
            break;
        case summer:
            System.out.println("夏天...");
            break;
        case autumn:
            System.out.println("秋天...");
            break;
        case winter:
            System.out.println("冬天...");
            break;      
    }       
}
}//switch里面直接放枚举值 不要加枚举类名字

————————————————————————————————————————————————————————————————————————————
枚举用法三:枚举类有方法  枚举值和参数绑定 构造函数是private

enum Gender{
//  public static final Sex man = new Sex("man");
    要重写Gender才行 用来覆盖无参的构造函数
    man("男"),woman("女");

    String value;  //成员 变量

    private Gender(String value){
        this.value = value;
    }
}

————————————————————————————————————————————————————————————————————————

//**枚举用法四:覆盖枚举的方法**
enum Sex{

    //  public static final Sex man = new Sex("man");
    man("男"){//匿名类

        @Override
        public void run() {
            System.out.println("男人在跑...");
        }

    },woman("女"){

        @Override
        public void run() {
            System.out.println("女人在跑...");
        }


    }; //枚举值

    String value; //成员 变量  public类型的


    //  public static final Sex man = new Sex();


    //构造函数
    private Sex(String  value){
        this.value = value;
    }

    //成员函数
    public void getValue(){
        System.out.println("value :"+ value);
    }

    public abstract void run();

}

——————————————————————————————————————————————————————————————————————————————

用法五:实现接口
public interface Behaviour {  
    void print();  
    String getInfo();  
}  
public enum Color implements Behaviour{  
    RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);  
    // 成员变量  
    private String name;  
    private int index;  
    // 构造方法  
    private Color(String name, int index) {  
        this.name = name;  
        this.index = index;  
    }  
//接口方法  
    @Override  
    public String getInfo() {  
        return this.name;  
    }  
    //接口方法  
    @Override  
    public void print() {  
        System.out.println(this.index+":"+this.name);  
    }  
}  

——————————————————————————————————————————————————————————————————————————————————————

用法六:使用接口组织枚举
public interface Food {  
    enum Coffee implements Food{  
        BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO  
    }  
    enum Dessert implements Food{  
        FRUIT, CAKE, GELATO  
    }  
}

Switch applicable data types: byte \ char \short \ int \ String \ enumeration type

Note:
For the enumeration value following the case statement in switch, you only need to write the enumeration value alone, and you do not need to declare which enumeration class the enumeration value belongs to.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326006454&siteId=291194637