JAVA基础知识|枚举

将代码中经常使用的常量,放在枚举中,是一个很好的编码习惯。便于统一修改,同时也增强代码的严谨和稳定性。当然,枚举的应用有很多,这里我只做一个简单的演示,以后看到有趣的使用,会慢慢丰富

package com.my.po;

/**
 * description:{description}
 * author:jyy
 * date:2018-02-07 17:20
 * modify:{modify}
 */
public enum Size {

    SMALL("S", "1"), MEDIUM("M", "2"), LARGE("L", "3"), EXTRA_LARGE("XL", "4");

    private String abbreviation;
    private String index;

    Size(String abbreviation, String index) {
        this.abbreviation = abbreviation;
        this.index = index;
    }

    public String getAbbreviation() {
        return abbreviation;
    }

    public String getIndex() {
        return index;
    }
}

分析:

SMALL("S","1")执行构造函数Size(String abbreviation,String index)

getAbbreviation()方法获取SMALL("S","1")中的S值

getIndex()方法获取SMALL("S","1")中的1值

    @Test
    public void test() {

        //查询SMALL的值
        System.out.println(Size.SMALL.toString());

        //toString()方法的逆方法valueOf(),s=Size.SMALL
        Size s = Enum.valueOf(Size.class, "SMALL");
        System.out.println(s.toString());

        //获取SMALL中的abbreviation,index
        System.out.println(Size.SMALL.getAbbreviation());
        System.out.println(Size.SMALL.getIndex());
    }

执行结果:

SMALL
SMALL
S
1

 这两天无意之间看到一个枚举类TimeUnit,里面的部分代码如下:

public enum TimeUnit {
    /**
     * Time unit representing one thousandth of a microsecond
     */
    NANOSECONDS {
        public long toNanos(long d)   { return d; }
        public long toMicros(long d)  { return d/(C1/C0); }
        public long toMillis(long d)  { return d/(C2/C0); }
        public long toSeconds(long d) { return d/(C3/C0); }
        public long toMinutes(long d) { return d/(C4/C0); }
        public long toHours(long d)   { return d/(C5/C0); }
        public long toDays(long d)    { return d/(C6/C0); }
        public long convert(long d, TimeUnit u) { return u.toNanos(d); }
        int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
    },

    。。。略。。。

    public long toNanos(long duration) {
        throw new AbstractMethodError();
    }

    /**
     * Equivalent to
     * {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}.
     * @param duration the duration
     * @return the converted duration,
     * or {@code Long.MIN_VALUE} if conversion would negatively
     * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
     */
    public long toMicros(long duration) {
        throw new AbstractMethodError();
    }

    。。。略。。。
}

最初不是很理解,后来在网上看到一个对枚举原理解释的帖子,顿时豁然开朗,下面我将简单介绍一下。

【举例】

public enum Size {

    SMALL("S","1"){
        public String getSize(){
            return "小号";
        }
        public String getRange(){
            return "5-10";
        }
    },
    MEDIUM("M","2"){
        public String getSize(){
            return "中号";
        }
        public String getRange(){
            return "11-20";
        }
    },
    LARGE("L","3"){
        public String getSize(){
            return "大号";
        }
        public String getRange(){
            return "21-30";
        }
    };

    public abstract String getSize();
    public abstract String getRange();

    private String abbreviation;
    private String index;

    Size(String abbreviation, String index) {
        this.abbreviation = abbreviation;
        this.index = index;
    }

    public String getAbbreviation() {
        return this.abbreviation;
    }

    public String getIndex() {
        return this.index;
    }
}
        System.out.println(Size.SMALL.getAbbreviation());
        System.out.println(Size.SMALL.getIndex());
        System.out.println(Size.SMALL.getSize());
        System.out.println(Size.SMALL.getRange());

执行结果:

S
1
小号
5-10

当声明枚举类型Size的时候,其实是声明一个抽象类Size,同时也声明了抽象方法getSize()、getRange()。SMALL、MEDIUM、LARGE都是Size的匿名内部类(由satic final 关键字修饰),并重写了抽象方法。

可以将上面的枚举类Size,改写成以下方式:

public abstract class Size {

    public static final Size SMALL = new Size("S", "1") {
        public String getSize() {
            return "小号";
        }

        public String getRange() {
            return "5-10";
        }
    };
    public static final Size MEDIUM = new Size("M", "2") {
        public String getSize() {
            return "中号";
        }

        public String getRange() {
            return "11-20";
        }
    };
    public static final Size LARGE = new Size("L", "3") {
        public String getSize() {
            return "大号";
        }

        public String getRange() {
            return "21-30";
        }
    };

    public abstract String getSize();

    public abstract String getRange();

    private String abbreviation;
    private String index;

    Size(String abbreviation, String index) {
        this.abbreviation = abbreviation;
        this.index = index;
    }

    public String getAbbreviation() {
        return this.abbreviation;
    }

    public String getIndex() {
        return this.index;
    }
}
原文地址:https://www.cnblogs.com/maikucha/p/8427394.html