java enum<<操作者

时间:2014-03-26 18:55:23

标签: java enums

我们期望为下面的枚举类提供什么样的值。困惑于<<运营商

public enum FrequencyEnum {

    DAILY       (1), 
    WEEKLY      (1 << 1), 
    BI_WEEKLY   (1 << 2), 
    MONTHLY     (1 << 3), 


    private short id;


    private FrequencyEnum(int id) {
        this.id = (short) id;
    }

    public short getId() {
        return id;
    }

    public void setId(short id) {
        this.id = id;
    }

    public static FrequencyEnum valueOf(short id) {
        for (FrequencyEnum status : FrequencyEnum.values()) {
            if (status.getId() == id) {
                return status;
            }
        }
        return null;
    }

}

1 个答案:

答案 0 :(得分:0)

<<是左移算子。在此上下文中,您将各种整数值传递给枚举构造函数(如kiruwka所述)。

请参阅:

http://en.wikipedia.org/wiki/Bitwise_operation http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html