Home >Java >JavaBase >What are the usages of java enumeration type enum?

What are the usages of java enumeration type enum?

coldplay.xixi
coldplay.xixiOriginal
2020-10-30 13:53:5821897browse

The usage of java enumeration type enum is: 1. Enumeration type constant; 2. Enumeration with one parameter; 3. Add new methods to the enumeration; 4. Override the enumeration method.

What are the usages of java enumeration type enum?

The usage of java enumeration type enum is:

1. Enumeration type constants

package com.yang;
//首先枚举是一个特殊的class
//这个class相当于final static修饰,不能被继承
//他的构造方法强制被私有化,下面有一个默认的构造方法private ColorEnum();
//所有的枚举都继承自java.lang.Enum类。由于Java 不支持多继承,所以枚举对象不能再继承其他类
public enum ColorEnum {
    //每个枚举变量都是枚举类ColorEnum的实例,相当于RED=new ColorEnum(1),按序号来。
    //每个成员变量都是final static修饰
    RED, GREEN, BLANK, YELLOW;
}

Test class:

@org.junit.Test
public void ColorTest() {
    //ordinal返回枚举变量的序号
    for(ColorEnum color:ColorEnum.values()) {
        System.out.println(color+",ordinal:"+color.ordinal()+",name:"+color.name());
    }
}

2. Enumeration with one parameter

package com.yang;
public enum TypeEnum {
    FIREWALL("firewall"),  
    SECRET("secretMac"),  
    BALANCE("f5");  
    private String typeName;  
    TypeEnum(String typeName) {  
        this.typeName = typeName;  
    }  
    /** 
     * 根据类型的名称,返回类型的枚举实例。 
     * 
     * @param typeName 类型名称 
     */  
    public static TypeEnum fromTypeName(String typeName) {  
        for (TypeEnum type : TypeEnum.values()) {  
            if (type.getTypeName().equals(typeName)) {  
                return type;  
            }  
        }  
        return null;  
    }  
    public String getTypeName() {  
        return this.typeName;  
    }    
}

Test class:

@org.junit.Test
public void TypeTest() {
    String typeName = "f5";  
    TypeEnum type = TypeEnum.fromTypeName(typeName);
    //type:是TypeEnum类实例化对象     typeName:实例化对象type的值             
    // ordinal:实例化对象type的序号(int)          排序值(默认自带的属性 ordinal 的值)
    //name:实化对象的名字(String)                            枚举名称(即默认自带的属性 name 的值)
    System.out.println(type+",typeName:"+type.getTypeName()+",ordinal:"+type.ordinal()+",name:"+type.name());
}

3. Add new methods to the enumeration

package com.yang;
public enum SeasonEunm {
    //每一个枚举变量都是枚举类SeasonEunm的实例化
    //SeasonEunm.SPRING获得的对象相当于new SeasonEunm("春","春困");获得的对象
    SPRING("春","春困"),SUMMER("夏","夏睡"),AUTUMN("秋","秋乏"),WINTER("冬","冬眠");
    //强制私有化的构造方法,因为枚举类默认继承Eunm类,被final static修饰,不能被继承
    //由于被强制private,所以不能使用new去生成枚举对象
    //在SeasonEunm.SPRING获得对象,隐式调用了下面的构造方法
    // 构造方法 ,赋值给成员变量
    private SeasonEunm(String name, String value) {
        this.name = name;
        this.value = value;
    }
    //添加成员变量的原因是,方便够着方法赋值,使用SeasonEunm.SPRING.getName就能获取对应的值
    private String name;
    private String value;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }

Test class:

@org.junit.Test
public void SessionTest() {
    //ordinal返回枚举变量的序号
    for(SeasonEunm seasion:SeasonEunm.values()) {
        System.out.println(seasion+",ordinal:"+seasion.ordinal()+",name:"+seasion.name());
        System.out.println("枚举变量值:"+seasion.getName()+",枚举变量值:"+seasion.getValue());
    }
}

4. Override the enumeration method

package com.yang;
public enum RGBEnum {
    RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);  
    // 成员变量  
    private String name;  
    private int index;  
    // 构造方法 ,赋值给成员变量
    private RGBEnum(String name, int index) {  
        this.name = name;  
        this.index = index;  
    }  
    //覆盖方法  :只能使用toString方法来输出枚举变量值
    @Override  
    public String toString() {  
        return this.index+"_"+this.name;  
    }  
}

Test method:

@org.junit.Test
public void RGBEnumTest() {
    for(RGBEnum rgb:RGBEnum.values()) {
        System.out.println(rgb.toString());
    }
}

Related free learning recommendations: java basic tutorial

The above is the detailed content of What are the usages of java enumeration type enum?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn