简体   繁体   中英

Java: Can I use two different names in an enum to count as the same thing?

I have an enum class with the cardinal directions(North, East, South, West):

public enum Direction {
    NORTH,
    EAST,
    SOUTH,
    WEST;
}

Is there a way to be able to use multiple names for the same thing? For example something like this:

public enum Direction {
    NORTH or N,
    EAST or E,
    SOUTH or S,
    WEST or W;
}

In practice what I want is to be able and sign to a variable either N or NORTH and have the two operations be exactly the same.

Example:

Direction direction1=new Direction.NORTH;
Direction direction2=new Direction.N;
//direction1==direction2
public enum Direction {
  NORTH,
  EAST,
  SOUTH,
  WEST,
  ;

  // Convenience names.
  public static final Direction N = NORTH;
  public static final Direction E = EAST;
  public static final Direction S = SOUTH;
  public static final Direction W = WEST;
}

is legal, but "N" will not work with the auto-generated valueOf method. Ie Direction.valueOf("N") will throw an IllegalArgumentException instead of returning Direction.NORTH .

You also cannot write case N: . You have to use the full names in switch es whose value is a Direction .

Other than that, the abbreviated form should work just as well as the full version. You can use Direction.N in EnumSet s, compare it for equality Direction.N == Direction.NORTH , get its name() (which is "NORTH" ), import static yourpackage.Direction.N; , etc.

You could do something like this (East and West omitted).

public enum Direction {
    NORTH {
        @Override
        Direction getDirection() {
            return NORTH;
        }
    },
    N {
        @Override
        Direction getDirection() {
            return NORTH;
        }
    },
    SOUTH {
        @Override
        Direction getDirection() {
            return SOUTH;
        }
    },
    S {
        @Override
        Direction getDirection() {
            return SOUTH;
        }
    }   ;

    abstract Direction getDirection();
}

Then you could something like this

public void foo(Direction arg) {
  Direction d = arg.getDirection();
}

Then you will always be dealing with only NORTH, SOUTH, EAST, and WEST.

Hum, maybe having a "client Enum" with variables that hold the actual "meaningful Enum"?

public enum Direction {
    NORTH(BaseDirection.NORTH),
    N(BaseDirection.NORTH),
    EAST(BaseDirection.EAST),
    E(BaseDirection.EAST),
    SOUTH(BaseDirection.SOUTH),
    S(BaseDirection.SOUTH),
    WEST(BaseDirection.WEST),
    W(BaseDirection.WEST);

    private BaseDirection baseDirection;

    private Direction(BaseDirection baseDirection) {
        this.baseDirection = baseDirection;
    }

    public BaseDirection getBaseDirection() {
        return baseDirection;
    }           
}

public enum BaseDirection {
    NORTH,
    EAST,
    SOUTH,
    WEST;
}

Kinda overkill, but you can expose Direction to client code and use getBaseDirection for actual logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM