zoukankan      html  css  js  c++  java
  • Java枚举使用

     在JDK1.5 之前,我们定义常量都是: public static fianl.... 。

    现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法。

    1、不用枚举

    package First;
    
    public class Test {
    
        public static final int RED = 1; 
        public static final int Green = 3;  
        public static final int YELLOW = 2;
    
        
        public static void main(String[] args) {
            int value = Test.Green;
            System.out.println(value);
        }
    }


    2、用枚举:改变上面

    package First;
    
    public class LightTest {
    
        public enum Light{
            
            RED(1),Green(3),YELLOW(2);
            
            private int nCode;
            
            /*
             * 1. private类型的构造函数
             * 2. 给变量赋值
             */
            private Light(int nCode){
                this.nCode = nCode;
            }
            /*
             * 获取各个变量的值
             */
            public int getValue(){
                return nCode;
            }
            
        }
        
        public static void main(String[] args) {
            
            
            Light light = Light.Green;
            int value = light.getValue();
            System.out.println(value);
            
            System.out.println("----------------");
            
            //遍历
            Light[] lights = Light.values();
            for(Light a : lights){
                System.out.println(a);
                System.out.println(a.nCode);
            }
            
        }
        
        
    }

    3、用枚举:多个参数

    package First;
    
    public class LightTest {
    
        public enum Light{
            
            RED(1,"红"),Green(3,"绿"),YELLOW(2,"黄");
            
            private int nCode;
            private String str;
            
            /*
             * 1. private类型的构造函数
             * 2. 给变量赋值
             */
            private Light(int nCode, String str){
                this.nCode = nCode;
                this.str = str;
            }
            /*
             * 获取各个变量的值
             */
            public int getValue(){
                return nCode;
            }
            
            public String getStr(){
                return str;
            }
            
        }
        
        public static void main(String[] args) {
            
            Light light = Light.Green;
            int value = light.getValue();
            String str = light.getStr();
            
            System.out.println(value);
            System.out.println(str);
            
        }
        
        
    }

    根据code 获取value

    public static String getValueByCode(int code) {
        for (SettleUserInfoEnum ele : values()) {
            if(ele.getCode() == code) {
                return ele.getValue();
            }
        }
        return null;
    }
  • 相关阅读:
    Hibernate笔记
    Struts2笔记(学struts2只需要这一篇文章)
    Linux开机启动和登录时各个文件的执行顺序
    Java虚拟机之垃圾回收算法思想总结
    码农雷林鹏:php概述
    码农雷林鹏:php教程
    雷林鹏分享:JDBC驱动类型
    码农雷林鹏教程分享:JDBC实例代码
    雷林鹏分享:JDBC环境设置
    雷林鹏分享:JDBC SQL语法
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/7723951.html
Copyright © 2011-2022 走看看