JAVA之enmu类详解

目录
    一、简介
    二、默认枚举类
    三、多值枚举类
    四、属性和方法
    五、构造函数
    六、重要方法
    七、引用参考

一、简介
    1、枚举类代表一组常量;
    2、枚举常量隐性声明为final static类型;
    3、每一个枚举常量代表了一个枚举类的对象;
    4、枚举类隐性实现了java.lang.Comparable接口和java.io.Serializable接口
    5、枚举类可以包涵多个构造函数

二、默认枚举类
    枚举类首先定义常量,其次才是属性、构造函数和方法。(注:常量名要大写)

     //创建默认枚举类
     public enum DefaultEnum {
        DEMOENUM1, DEMOENUM2, DEMOENUM3;
    }
    
    //访问枚举对象
    public class client {
        public static void main(String[] args) {
            DefaultEnum demoEnum = DefaultEnum.DEMOENUM1;
            System.out.println(demoEnum);//输出:DEMOENUM1
        }
    }

三、多值枚举对象
    枚举对象可以包涵一个或多个值,值的类型可以任意基本类型。枚举对象的值在枚举类中定义后可定义setter方法修改,但是如非必要不要这么做,因为这会违背枚举对象常量的本质。

    //创建枚举类
    public enum MutiValueEnum {
    
        DEMOENUM1(1, "hello"),
        DEMOENUM2(2, "enum");
        
        //枚举对象的变量
        private int id;
        private String value;
        
        //重写枚举类的默认构造器
        MutiValueEnum(int id, String value) {
            this.id = id;
            this.value = value;
        }
        
        //获得id属性的值
        public int getId() { return this.id; }
        
        //获得value属性的值
        public String getValue() { return this.value; }
    }
    
    //访问枚举对象
    public class client {
        public static void main(String[] args) {
            MutiValueEnum mutiValueEnum = MutiValueEnum.DEMOENUM1;
            
            System.out.println(mutiValueEnum);//输出:DEMOENUM1
            System.out.println(mutiValueEnum.getId());//输出:1
            System.out.println(mutiValueEnum.getValue());//输出:hello
        }
}

四、属性和方法
    枚举类里的属性和方法是属于枚举常量的,所以枚举常量可以调用这些方法。我们可以在枚举类定义其他我们所需要的方法。其实,枚举类同正常类有许多相似之处,对于变量和方法,我们可以按照正常类里的属性和方法去使用,只不过我们不能通过new来创建对象,而是枚举类自行创建。注:枚举类里不能有抽象方法。
   

    //测试枚举类的属性和方法
    public enum TestAttributeAndMethodOfEnum {
        DEMOENUM;
        
        private int id;
        private String value;
        private boolean flag;
        
        //setter and getter
//other method
        public void TestMethod1() { … }
        public void TestMethod2() { … }
    
    }
    
    //测试类
    public class Client {
        public static void main(String[] args) {
            TestAttributeAndMethodOfEnum testEnum = TestAttributeAndMethodOfEnum.DEMOENUM;
            
            testEnum.setId(1);
            testEnum.TestMethod1();
            …
        }
    
    }

五、构造函数
    同正常类一样,枚举类可以拥有多个构造函数。构造函数的参数要同枚举常量的值的数量相一致。并且枚举类的构造函数的可见性只能设为包可见或是私有,其次也不能通过new来创建枚举对象。由枚举类的使用类负责枚举对象的构建。

    enum Color 
    { 
        RED, 
        GREEN("light"),
        BLUE(255,23,223); 
        
        private String level;
        private int r,g,b;
        
        //默认无参构造,在此可省略
        //Color() { }
        
        //对应一个值的GREEN常量
        Color(String level) {
            this.level = level;
        }
        //对应三个值的BLUE常量
        Color(int r, int g, int b) {
            this.r = r;
            this.g = g;
            this.b = b;
        }
    } 
    public class Test 
    {
        // Driver method 
        public static void main(String[] args) 
        { 
            Color c1 = Color.RED; 
            Color c2 = Color.GREEN;
            Color c3 = Color.BLUE;
            System.out.println(c1); 
            …
        } 
    }
    在上述例子中,枚举类会被内部的转换为
    class Color
  {
     public static final Color RED = new Color();
     public static final Color GREEN = new Color("light");
     public static final Color BLUE = new Color(255, 23, 223);
  }

六、重要方法
    values()                            返回枚举类的所有常量;
    ordinal()                           返回枚举常量的序号
    valueOf(String name)      返回名为name的枚举常量,如果存在。

    // 演示 values(), ordinal() 和 valueOf() 方法
    enum Color 
    { 
        RED, GREEN, BLUE; 
    } 
    
    public class Test 
    { 
        public static void main(String[] args) 
        { 
            Color arr[] = Color.values(); 
    
            for (Color col : arr) 
            { 
                System.out.println(col + " at index "
                                + col.ordinal()); 
            } 
    
            System.out.println(Color.valueOf("RED"));
            //此行代码会返回IllegalArgumentException 异常,因为Color枚举类里没有名为WHITE的常量
            System.out.println(Color.valueOf("WHITE")); 
        } 
    } 

参考文献
    https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
    https://www.geeksforgeeks.org/enum-in-java/
    https://dzone.com/articles/java-enums-how-to-make-much-more-useful
    https://stackoverflow.com/questions/19600684/java-enum-with-multiple-value-types
    https://docs.oracle.com/javase/8/docs/api/

猜你喜欢

转载自www.cnblogs.com/climy/p/10718501.html