Java 枚举<<运算符



对于像下面这样的枚举类,我们期望得到什么样的值呢?困惑于<<操作符

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;
    }
}

<<为左移运算符。在此上下文中,您将向枚举构造函数传递各种整数值(如kiruwka所提到的)。

:

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

相关内容

  • 没有找到相关文章

最新更新