枚举的详解

简介: 枚举的详解

枚举

枚举常量的修饰符会省略。实际的修饰符是 final static。final表示不可以被继承,static表示是类成员常量。
枚举的好处:
    1、类型安全
      2、紧凑有效的数据定义
      3、可以和程序其他部分完美交互
      4、运行效率高

1、定义枚举类

public enum Constants_01 {
    //定义枚举类成员常量
    CONSTANTS_A,
    CONSTANTS_B,
    CONSTANTS_C;
}

2、展示枚举成员常量

public class ShowEnum_02 {
    enum Constants{
        Constants_A,Constants_B
    }
    public static void main(String[] args) {
        //Constants.values()  获取所有枚举类所有的成员,并且以数组返回
        for (int i = 0; i < Constants.values().length; i++) {
            System.out.println("枚举类型的成员变量:"+Constants.values()[i]);
        }
    }
}

3、枚举类型之间的比较(比较的枚举常量的位置)

public class EnumMethodTest_03 {
    //定义比较枚举类型方法,参数类型为枚举
    public static void compare(ShowEnum_02.Constants constants){
        for (int i = 0; i < ShowEnum_02.Constants.values().length; i++) {
            System.out.println(constants+"与"+ShowEnum_02.Constants.values()[i]+"的比较结果为:"+constants.compareTo(ShowEnum_02.Constants.values()[i]));
        }
    }
    public static void main(String[] args) {
        //在同一个类中,可以互相调用方法,又因为是static,所以可以直接用方法名
        compare(ShowEnum_02.Constants.valueOf("Constants_A"));
    }
}

4、获取枚举类型在索引的位置

public class EnumIndexTest_04 {
    enum Constants{
        //将常量放置在枚举类型
        Constant_A,Constant_B,Constant_C
    }
    public static void main(String[] args) {
        for (int i = 0; i < Constants.values().length; i++) {
            System.out.println(Constants.values()[i]+"在枚举类型中索引位置为:"+Constants.values()[i].ordinal());
        }
    }
}

4、获取枚举类型在索引的位置

public class EnumStructTest_05 {
    enum Constants{
        Constant_A("我是枚举成员A"),
        Constant_B("我是枚举成员B"),
        Constant_C("我是枚举成员C"),
        Constant_D(3);
        private String description;
        private int i=4;
        private Constants(){
        }
        private Constants(String description){
            this.description=description;
        }
        private Constants(int i){
            this.i=this.i+i;
        }
        public String getDescription() {
            return description;
        }
        public int getI() {
            return this.i;
        }
    }
    public static void main(String[] args) {
        for (int i = 0; i < Constants.values().length; i++) {
            System.out.println(Constants.values()[i]+"调用getDescription()方法为:"+Constants.values()[i].getDescription());
        }
        System.out.println(Constants.valueOf("Constant_D")+"调用getI()方法为"+Constants.valueOf("Constant_D").getI());
    }
}

5、枚举练习

//定义一个枚举类,使用switch语句获取枚举类型的值
public class Practice_01 {
    enum Constant{
        constant_A,
        constant_B,
        constant_C
    }
   public void doit(Constant c){
       switch (c){
           case constant_A:
               System.out.println("A");
               break;
           case constant_B:
               System.out.println("B");
               break;
           case constant_C:
               System.out.println("C");
               break;
       }
   }
    public static void main(String[] args) {
        new Practice_01().doit(Constant.constant_C);
    }
}

6、总结

个人觉得枚举很安全,在大家觉得使用某个类的成员属性是固定的话,但是又不想要去处理很多异常输入的情况,就固定为枚举类,使用的时候就只能使用枚举类中定义的成员属性去匹配内容吗,这样可以减少很多if语句嘞。

相关文章
|
2月前
|
安全 Java 索引
枚举的使用
枚举的使用
|
3月前
|
Java C语言 Spring
枚举
【2月更文挑战第4天】枚举。
29 4
|
存储 JavaScript 前端开发
枚举(enumerables)
枚举(enumerables)
88 0
|
算法 安全 小程序
使用枚举的正确姿势
使用枚举的正确姿势
239 0
使用枚举的正确姿势
|
编解码 网络协议 5G
【C/C++】一文了解枚举使用
在数学和计算机科学理论中,一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计数。通俗来讲,枚举是具有共同属性的一类整数常数的有限集合,例如星期一到星期天,就是一个成员个数为7的枚举类型,枚举成员也称为枚举项。
115 0
【C/C++】一文了解枚举使用
|
安全 Java 程序员
为什么建议你使用枚举?(下)
为什么建议你使用枚举?
204 0
为什么建议你使用枚举?(下)
|
Java C# Python
为什么建议你使用枚举?(上)
为什么建议你使用枚举?
120 0
为什么建议你使用枚举?(上)
|
存储 Java
为什么建议你使用枚举?(中)
为什么建议你使用枚举?
112 0
|
Java Spring 安全
Java基础巩固-枚举的使用
枚举:jdk1.5引入的新特性,允许用常量表示特定的数据片段,且全一类型安全的形式来表示。 常用的定义常量方式 public static final xxx .
1074 0