博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中关于枚举的7种用法
阅读量:6441 次
发布时间:2019-06-23

本文共 2826 字,大约阅读时间需要 9 分钟。

1.定义常量:

public enum Color {    RED,ORANGE,YELLOW,GREEN,INDIGO,BLUE,PURPLE}

2.用于switch: 

enum Color {    RED,ORANGE,YELLOW,GREEN,INDIGO,BLUE,PURPLE}public class Draw{    Color c = Color.BLUE;    public void draw(){        switch(c){        case RED:            c = Color.RED;            break;        case ORANGE:            c = Color.ORANGE;            break;        case YELLOW:            c = Color.YELLOW;            break;        case GREEN:            c = Color.GREEN;            break;        case INDIGO:            c = Color.INDIGO;            break;        case BLUE:            c = Color.BLUE;            break;        case PURPLE:            c = Color.PURPLE;            break;        }    }} 

3.在枚举中添加新方法:

public enum Color {    RED(1,"红"),    ORANGE(2,"橙"),    YELLOW(3,"黄"),    GREEN(4,"绿"),    INDIGO(5,"靛"),    BLUE(6,"蓝"),    PURPLE(7,"紫");    //成员变量    private int sequence;    private String name;    //构造方法    private Color(int sequence, String name) {        this.sequence = sequence;        this.name = name;    }    //自定义方法    public static String getColorName(int sequence){        for (Color c : Color.values()) {            if(c.getSequence() == sequence)                return c.name;        }        return null;    }    //getter&setter    public int getSequence() {        return sequence;    }    public void setSequence(int sequence) {        this.sequence = sequence;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

4.覆盖(重写)枚举的方法:

public enum Color {    RED(1,"红"),    ORANGE(2,"橙"),    YELLOW(3,"黄"),    GREEN(4,"绿"),    INDIGO(5,"靛"),    BLUE(6,"蓝"),    PURPLE(7,"紫");    //成员变量    private int sequence;    private String name;    //构造方法    private Color(int sequence, String name) {        this.sequence = sequence;        this.name = name;    }    //覆盖方法    @Override    public String toString() {        return "顺序:"+this.sequence+"_颜色名:"+this.name;    }}

5.实现接口:

interface Action{    void getDetail();}public enum Color implements Action{    RED(1,"红"),    ORANGE(2,"橙"),    YELLOW(3,"黄"),    GREEN(4,"绿"),    INDIGO(5,"靛"),    BLUE(6,"蓝"),    PURPLE(7,"紫");    //成员变量    private int sequence;    private String name;    //构造方法    private Color(int sequence, String name) {        this.sequence = sequence;        this.name = name;    }    @Override    public void getDetail() {        System.out.println("顺序:"+this.sequence+"_颜色名:"+this.name);    }}

6.使用接口组织枚举:

public interface Food {      enum Coffee implements Food{          BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO      }      enum Dessert implements Food{          FRUIT, CAKE, GELATO      }  }

7.枚举集合:

java.util.EnumSet和java.util.EnumMap是两个枚举集合。EnumSet保证集合中的元素不重复;EnumMap中的key是enum类型,而value则可以是任意类型。

 

转载于:https://www.cnblogs.com/lxcmyf/p/6558514.html

你可能感兴趣的文章
37Exchange 2010升级到Exchange 2013-测试邮件流(未切换公网发布)
查看>>
Solr6.6.0 自动生成ID
查看>>
Spring框架是什么,有哪些优点
查看>>
How To Manage Dell Servers using OMSA – OpenManage Server Administrator On Linux
查看>>
陈松松:视频营销老手给新手最忠诚的10条建议
查看>>
抽象类
查看>>
Linux的学习之路
查看>>
VMware安装Mac OS X
查看>>
Segmentation fault(Core Dump)
查看>>
Ext Dom Query
查看>>
Microsoft Visio 组件Aspose.Diagram 10月新版17.10发布 | 包含.NET 和Java
查看>>
谷歌明确Fuchsia并非基于Linux内核
查看>>
即刻搜索调整
查看>>
MyEclipse 7.0快捷键ALT+/没用
查看>>
整合apache和tomcat构建Web服务器
查看>>
Microsoft word/Excel/PPT打开报错问题
查看>>
vue的video插件vue-video-player
查看>>
LR 性能分析指标解释
查看>>
Linux中用户和组的介绍及其相关命令
查看>>
IOS 自定义UISwitch
查看>>