Java:我可以在枚举中使用两个不同的名称来算作同一事物吗?

发布于 2024-11-05 23:57:46 字数 470 浏览 6 评论 0原文

我有一个带有基本方向(北、东、南、西)的枚举类:

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

有没有办法能够对同一事物使用多个名称?例如这样的事情:

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

在实践中,我想要的是能够对变量 N 或 NORTH 进行签名,并使这两个操作完全相同。

例子:

Direction direction1=new Direction.NORTH;
Direction direction2=new Direction.N;
//direction1==direction2

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒眉千度 2024-11-12 23:57:46
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;
}

是合法的,但 "N" 不适用于自动生成的 valueOf 方法。即 Direction.valueOf("N") 将抛出 IllegalArgumentException 而不是返回 Direction.NORTH

您也不能编写case N:。您必须在值为 Directionswitch 中使用全名。

除此之外,缩写形式应该与完整版本一样有效。您可以在 EnumSet 中使用 Direction.N,比较其是否相等 Direction.N == Direction.NORTH,获取其名称()(即"NORTH")、import static yourpackage.Direction.N;等。

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. I.e. 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 switches 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 EnumSets, compare it for equality Direction.N == Direction.NORTH, get its name() (which is "NORTH"), import static yourpackage.Direction.N;, etc.

预谋 2024-11-12 23:57:46

你可以做这样的事情(省略了东和西)。

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

那么你可以像这样

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

然后你将永远只处理北、南、东和西。

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.

九命猫 2024-11-12 23:57:46

嗯,也许有一个“客户端枚举”,其中包含保存实际“有意义的枚举”的变量?

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

有点矫枉过正,但您可以将 Direction 公开给客户端代码并使用 getBaseDirection 来实现实际逻辑。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文