zoukankan      html  css  js  c++  java
  • java 枚举的常见使用方法

    JDK1.5引入了新的类型-枚举,枚举的出现在日常开发中带来了极大的方便。

    常用方法一:常量

      JDK1.5之前我们平时定义系统常量,基本都是用public static final ... 出现枚举以后我们可以讲枚举封装在枚举中。

    public enum Color {
        YELLOR,RED,BALCK
    }

    常用方法二:Switch

    public class Test {
        
        public static void main(String[] args) {
            Light color = Light.GREEN;
            System.out.println(change(color));
        }
      /**
         * 枚举switch
         * 
         * @param color
         * @return
         */
        private static String change(Light color) {
            String remind = null;
            switch (color) {
                case RED:
                    color = Light.GREEN;
                    remind = "红灯停";
                    break;
                case GREEN:
                    color = Light.GREEN;
                    remind = "绿灯行";
                    break;
                case YELLOR:
                    color = Light.YELLOR;
                    remind = "黄灯亮了等一等";
                    break;
                default:
                    break;
            }
            return remind;
        }
    }

     常用方法三:枚举遍历

    enum Light {
        YELLOR, RED, GREEN
    }
    
    public class Test {
    
        public static void main(String[] args) {
            EnumIterator();
        }
    
        /**
         * 枚举遍历
         */
        private static void EnumIterator() {
            
            for (Light light : Light.values()) {
                if (light.equals(Light.GREEN)) {
                    System.out.println("绿灯行");
                } else if (light.equals(Light.RED)) {
                    System.out.println("红灯停");
                } else if (light.equals(Light.YELLOR)) {
                    System.out.println("黄灯亮了等一等");
                }
            }
        }
    }

      常用方法四:枚举类中添加新方法

    public enum CountryICON {
    
        China("中国", "china.icon"), USA("美国", "usa.icon"),;
    
        private String country;
    
        private String icon;
    
        private CountryICON(String country, String icon) {
            this.country = country;
            this.icon = icon;
        }
    
        public String getCountry() {
            return country;
        }
    
        public void setCountry(String country) {
            this.country = country;
        }
      //通过输入国家名获取所对应的icon
        public static String getIcon(String country) {
            for (CountryICON countryIcon : CountryICON.values()) {
                if (countryIcon.getCountry().equals(country)) {
                    return countryIcon.icon;
                }
            }
            return null;
        }
    
        public void setIcon(String icon) {
            this.icon = icon;
        }
    
    }
  • 相关阅读:
    C语言输出颜色
    嵌入式Linux串口编程简介
    推荐:实现RTSP/RTMP/HLS/HTTP协议的轻量级流媒体框架,支持大并发连接请求
    嵌入式串口打印信息重定向到当前终端界面
    C、C++、boost、Qt在嵌入式系统开发中的使用
    LInux下Posix的传统线程示例
    Linux用C语言模拟‘ls‘命令
    关于Linux目录访问函数总结
    Inter内部指令--AVX编程基础
    SPECCPU2006测试(456测试小记)
  • 原文地址:https://www.cnblogs.com/parryyang/p/5659531.html
Copyright © 2011-2022 走看看