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;
        }
    
    }
  • 相关阅读:
    hlgoj 1766 Cubing
    Reverse Linked List
    String to Integer
    Bitwise AND of Numbers Range
    Best Time to Buy and Sell Stock III
    First Missing Positive
    Permutation Sequence
    Next Permutation
    Gray Code
    Number of Islands
  • 原文地址:https://www.cnblogs.com/parryyang/p/5659531.html
Copyright © 2011-2022 走看看