2 枚举

1 基本使用

package com.cowthan.enum2;

public enum Planet {
    
    MERCURY(3.302e+23, 2.439e6),  //水星
    VENUS(4.869e+24, 6.052e6),  //金星
    EARTH(5.975e+24, 6.378e6),  //
    MARS(6.419e+23, 3.393e6),   //火星
    JUPITER(1.899e+27, 7.149e7), //木星
    SATURN(5.685e+26, 6.027e7), //土星
    URANUS(8.683e+25,  2.556e7), //天王星
    NEPTUNE(1.024e+26, 2.477e7); //海王星
    
    /*
     
     枚举天生不可变,因此所有域都应该是final的
      
     */
    private final double mass;   //  kg
    private final double radius; //  meter
    private final double surfaceGravity; //    m/s^2
    public boolean fuck = false;
    
    private static final double G = 6.67300E-11;
    
    Planet(double mass, double radius) {
        this.mass = mass;
        this.radius = radius;
        surfaceGravity = G * mass / (radius * radius);
    }
    
    public double mass() {return mass;}
    public double radius() { return radius;}
    public double surfaceGravity() { return surfaceGravity;}
    public double surfaceWeight(double mass){
        return mass * surfaceGravity;
    }
    
    
    public static void main(String[] args) {
        double earthWeight = 100; //kg
        double mass = earthWeight / Planet.EARTH.surfaceGravity();
        for(Planet p: Planet.values()){
            System.out.printf("Weight on %s is %f%n", p, p.surfaceWeight(mass));
        }
    }
}

public enum Operation_1 {

    PLUS, MINUS, TIMES, DIVIDE;
    
    public double apply(double x, double y){
        
        switch(this){
        case PLUS: return x+y;
        case MINUS: return x-y;
        case TIMES: return x*y;
        case DIVIDE: return x/y;
        }
        throw new AssertionError("unknown op: " + this);  ///没有这句,编译不会通过
    }
    
}

2 抽象方法

package com.cowthan.enum2;

/**
 * 特定于常量的方法实现
 *
 */
public enum Operation {

    PLUS("+") {
        @Override
        double apply(double x, double y) {
            return x + y;
        }
    }, 
    MINUS("-") {
        @Override
        double apply(double x, double y) {
            return x - y;
        }
    }, 
    TIMES("*") {
        @Override
        double apply(double x, double y) {
            return x * y;
        }
    }, 
    DIVIDE("/") {
        @Override
        double apply(double x, double y) {
            return x / y;
        }
    };
    
    private final String symbol;
    
    private Operation(String symbol) {
        this.symbol = symbol;
    }
    
    abstract double apply(double x, double y);
    
    @Override
    public String toString() {
        return symbol;
    }
}

3 enum操作

enum的静态方法:
Week[] values():返回所有常量的数组
Week valueOf(String):根据常量名,获得枚举常量


enum的实例方法:
name():获取常量名
ordinal():获取枚举常量在类型中的数字位置,从0开始


特别注意:
ordinal()是内置的,这个是序数,是为EnumSet和EnumMap设计的,程序员不应该依赖这个方法做有关下标的事

你应该用实例域代替序数,例如MONDAY(1)



4 EnumSet代替位域: 枚举的集合

什么是位域呢,就是常见的flag模式,或者标志位模式

public class Text{

    public static final int STYLE_BOLD          = 1 << 0; //1   0001
    public static final int STYLE_ITALIC        = 1 << 1; //2   0010
    public static final int STYLE_UNDERLINE     = 1 << 2; //4   0100
    public static final int STYLE_STRIKETHROUGH = 1 << 3; //8   1000
    
    public void applyStyles(int styles){
    }

}

text.applyStyles(STYLE_BOLD | STYLE_ITALIC);  

OR运算符可以将几个常量合并到一个集合中,
这就叫位域(bit field),可以认为位域是标志位的集合


EnumSet可以用单个long来实现,性能比得上位域

改为使用位域:

public class Text{
    public enum Style { BOLD, ITALIC, UNDERLINE, STRIKETHROUGH }
    
    //任何Set都可以传进来,但EnumSet是最佳的
    
    public void applyStyles(Set