Can I use bitwise OR for Java Enums

后端 未结 3 1927
别跟我提以往
别跟我提以往 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:10

    You can't use a bitwise or like that. Try something more like this:

    public boolean canGoDirection(Set dirs){
         return dirs.contains(Direction.North);
    }
    

    called like this:

    this.canGoDirection(EnumSet.of(Direction.South,Direction.North));
    

提交回复
热议问题