Switch 语句:数字到枚举值 / 1002 = MyEnum.NewYorkID

发布于 2024-12-01 09:02:20 字数 384 浏览 1 评论 0原文

好吧,我想让这个工作成功

public static void main(String[] args) {

    int cityId = 1234;

    switch (cityId) {
        case MOSCOW:
            System.out.println("Moscow");
            break;

        case SOCHI:
            break;  
}

public enum Test {
    MOSCOW,
    NEWYORK,
    SOCHI
}

所以我想将 Test 枚举与城市 id 链接起来,这可能吗?对于这种情况,最好的模式是什么?

谢谢你!

Well, I want to make this work

public static void main(String[] args) {

    int cityId = 1234;

    switch (cityId) {
        case MOSCOW:
            System.out.println("Moscow");
            break;

        case SOCHI:
            break;  
}

public enum Test {
    MOSCOW,
    NEWYORK,
    SOCHI
}

So I want to link Test enum with city ids, is that possible? What is the best pattern for this situation?

Thank you!

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

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

发布评论

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

评论(3

何处潇湘 2024-12-08 09:02:20

你不能将其混合在一个开关中。您可以将 Test 枚举传递到 switch 语句中,或者在 case 语句中使用常量 id。

我建议有一个映射 cityId <->在调用开关之前测试实例并进行转换。

像编辑这样的东西

Map<Integer, Test>` mapping = ...;
mapping.put(1234, Test.MOSCOW ); //note the use of autoboxing

...

mapping.get(cityId); //note the use of autoboxing

:请注意,您可以将此映射放入枚举中,将 cityId 字段添加到枚举中,并自动填充 values() 返回的数组中的映射,就像 Chris 的建议一样。

public enum Test {
   MOSCOW(1001),
   NEWYORK(1002),
   SOCHI(1234);

   private final int cityId;

   private Test(int cityId) {
    this.cityId = cityId;
   }

   ...


   private static Map<Integer, Test> mapping = new HashMap<Integer, Test>();

   static { //executed when the class is loaded
     for( Test t : values() ) {
        mapping.put(t.getCityId(), t);
     }
   }

   public static toTest(int cityId) {
     return mapping.get(cityId);
   }
}

这是我们经常为类似的事情做的事情。

You can't mix that in a switch. Either you pass a Test enum into the switch statement, or use the constant ids in the case statements.

I'd suggest having a mapping cityId <-> Test instance and converting before calling the switch.

Something like

Map<Integer, Test>` mapping = ...;
mapping.put(1234, Test.MOSCOW ); //note the use of autoboxing

...

mapping.get(cityId); //note the use of autoboxing

Edit: note that you could put this mapping into the enum, add a cityId field to the enum and automatically fill the mapping from the array returned by values(), much like Chris' suggestion.

public enum Test {
   MOSCOW(1001),
   NEWYORK(1002),
   SOCHI(1234);

   private final int cityId;

   private Test(int cityId) {
    this.cityId = cityId;
   }

   ...


   private static Map<Integer, Test> mapping = new HashMap<Integer, Test>();

   static { //executed when the class is loaded
     for( Test t : values() ) {
        mapping.put(t.getCityId(), t);
     }
   }

   public static toTest(int cityId) {
     return mapping.get(cityId);
   }
}

That's something we often do for similar things.

看轻我的陪伴 2024-12-08 09:02:20

您的代码的唯一问题是您需要打开 Test 而不是 int

IE:

public static void main(String[] args) {

    Test city = SOCHI;

    switch (city) {
        case MOSCOW:
            System.out.println("Moscow");
        break;

       case SOCHI:
           break;   
}

public enum Test{
    MOSCOW,
    NEWYORK,
    SOCHI
}

The only problem with your code is that you need to switch on Test not int.

ie:

public static void main(String[] args) {

    Test city = SOCHI;

    switch (city) {
        case MOSCOW:
            System.out.println("Moscow");
        break;

       case SOCHI:
           break;   
}

public enum Test{
    MOSCOW,
    NEWYORK,
    SOCHI
}
一萌ing 2024-12-08 09:02:20

您可以在枚举中添加一个 cityId 字段:

public enum Test {
    MOSCOW(1001),
    NEWYORK(1002),
    SOCHI(1234);

    private final int cityId;

    private Test(int cityId) {
        this.cityId = cityId;
    }

    public int getCityId() {
        return cityId;
    }

    public static Test valueOf(int cityId) {
        /*
         * Using linear search because there's only a small handful
         * of enum constants. If there's a huge amount (say, >20),
         * a map lookup is better.
         */
        for (Test value : values()) {
            if (value.cityId == cityId)
                return value;
        }
        throw new IllegalArgumentException("Unknown city ID: " + cityId);
    }
}

然后您确实可以打开枚举值:

switch (Test.valueOf(cityId)) {
case MOSCOW:
    // ...
}

You can add a cityId field in your enum:

public enum Test {
    MOSCOW(1001),
    NEWYORK(1002),
    SOCHI(1234);

    private final int cityId;

    private Test(int cityId) {
        this.cityId = cityId;
    }

    public int getCityId() {
        return cityId;
    }

    public static Test valueOf(int cityId) {
        /*
         * Using linear search because there's only a small handful
         * of enum constants. If there's a huge amount (say, >20),
         * a map lookup is better.
         */
        for (Test value : values()) {
            if (value.cityId == cityId)
                return value;
        }
        throw new IllegalArgumentException("Unknown city ID: " + cityId);
    }
}

Then you can indeed switch on the enum value:

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