枚举理解

package com.ming.test;
/* * 枚举理解: * enum都继承了Enum类,在创建Enum对象时都会调用Enum中默认的构造方法 * protected Enum(String name, int ordinal); * 创建枚举类的两种形式: * MON, TUE, WED, THU, FRI, SAT, SUN;// 默认的构造函数protected Enum(String name, int ordinal) * RED(255, 0, 0);//调用完默认的构造函数protected Enum(String name, int ordinal),继续调用当前枚举类的构造函数 */
public enum EnumT {
    MON, TUE, WED, THU, FRI, SAT, SUN;// 默认的构造函数protected Enum(String name, int ordinal)
    public static void main(String[] args) {
        System.out.println(EnumT.MON.ordinal());
        System.out.println(EnumT.MON);
    }
}

enum Color {
    RED(255, 0, 0), BLUE(0, 0, 255), BLACK(0, 0, 0), YELLOW(255, 255, 0), GREEN(0, 255, 0);
    // 构造枚举值,比如RED(255,0,0)
    private Color(int rv, int gv, int bv) {
        this.redValue = rv;
        this.greenValue = gv;
        this.blueValue = bv;
    }
    private Color(String rv, int gv, int bv) {
        this.greenValue = gv;
        this.blueValue = bv;
    }


    public String toString() { // 覆盖了父类Enum的toString()
        return super.toString() + "(" + redValue + "," + greenValue + "," + blueValue + ")";
    }

    private int redValue; // 自定义数据域,private为了封装。
    private int greenValue;
    private int blueValue;

    public static void main(String[] args) {
        Enum a = Color.RED;
        System.out.println(Color.RED.redValue);
    }
}

你可能感兴趣的:(enum)