Can I use bitwise OR for Java Enums

后端 未结 3 2025
别跟我提以往
别跟我提以往 2021-01-07 21:32

Suppose you have an enum Direction

enum Direction{
    North,South,East West
}

Could I write a method that uses bitwise or\'s to compare mu

3条回答
  •  情书的邮戳
    2021-01-07 22:24

    Not directly, but if you add a primitive (e.g. int) field to your enum, you can OR that int value.

    However, the resulting value is of int (which can not be implicitly converted to boolean, much less back to enum).

    Probably you would be better off using an EnumSet instead. This is much more idiomatic to Java.

提交回复
热议问题