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;
        }
    
    }
  • 相关阅读:
    sql日期操作
    用户活跃状态模型
    R基于Bayes理论实现中文人员特性的性别判定
    python的一点小常识
    [工作技能]SVN
    北大计算所词性标注集简表
    c++ boost 汉字和模式串混用的例子
    SVN文本文件报二进制属性的问题
    我的R代码备份
    FaceBook Twitter实习生简历求内推
  • 原文地址:https://www.cnblogs.com/parryyang/p/5659531.html
Copyright © 2011-2022 走看看