Java使用带有switch语句的枚举 [英] Java using enum with switch statement

查看:144
本文介绍了Java使用带有switch语句的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过与这个问题类似的各种问答,但是还没有找到解决方案。

I've looked at various Q&As on SO similar to this question but haven't found a solution.

我所拥有的是一个枚举,代表不同的查看电视指南的方式...

What I have is an enum which represents different ways to view a TV Guide...

在NDroid 应用程序 class

In the NDroid Application class

static enum guideView {
    GUIDE_VIEW_SEVEN_DAY,
    GUIDE_VIEW_NOW_SHOWING,
    GUIDE_VIEW_ALL_TIMESLOTS
}

...当用户更改视图时,事件处理程序从 int 0-2,我想做这样的事情...

...when the user changes the view an event handler receives an int from 0-2 and I'd like to do something like this...

在Android 活动 onClick(DialogInterface对话框,int which)事件处理程序

In an Android Activity onClick(DialogInterface dialog, int which) event handler

// 'which' is an int from 0-2
switch (which) {
    case NDroid.guideView.GUIDE_VIEW_SEVEN_DAY:
    ...
    break;
}

我习惯于C#枚举和select / case语句,这将允许某事像上面那样,我知道Java做的事情有所不同,但我根本无法理解我需要做的事情。

I'm used to C# enums and select/case statements which would allow something like the above and I know Java does things differently but I just can't make sense of what I need to do.

我要不得不诉诸于 if 语句?可能只有3个选择,所以我可以做到,但我想知道如何在Java中使用switch-case。

Am I going to have to resort to if statements? There will likely only ever be 3 choices so I could do it but I wondered how it could be done with switch-case in Java.

编辑对不起,我没有完全展开这个问题,因为我把它看作是一个通用的Java问题。我已经添加了这个问题来解释一下。

EDIT Sorry I didn't completely expand on the issue as I was looking at it as being a generic Java issue. I've added to the question to explain a bit further.

没有什么是Android特定的,这就是为什么我没有将其标记为Android,但是枚举在应用程序类中定义,而我不需要切换的代码位于活动中。枚举是静态的,因为我需要从多个活动中访问它。

There isn't anything that's Android specific which is why I didn't tag it as Android but the enum is defined in the Application class and the code where I wan't the switch is in an Activity. The enum is static as I need to access it from multiple Activities.

推荐答案

你所缺少的部分是从整数转换到类型安全的枚举。 Java不会自动执行。有几种方法可以解决这个问题:

The part you're missing is converting from the integer to the type-safe enum. Java will not do it automatically. There's a couple of ways you can go about this:


  1. 使用静态final int的列表,而不是类型安全的枚举,并打开您收到的int值(这是Java 5之前的方法)

  2. 打开指定的id值(如 guideView.GUIDE_VIEW_SEVEN_DAY.ordinal()

  3. 确定由int值表示的枚举值,然后打开枚举价值。

  1. Use a list of static final ints rather than a type-safe enum and switch on the int value you receive (this is the pre-Java 5 approach)
  2. Switch on either a specified id value (as described by heneryville) or the ordinal value of the enum values; i.e. guideView.GUIDE_VIEW_SEVEN_DAY.ordinal()
  3. Determine the enum value represented by the int value and then switch on the enum value.

enum GuideView {
    SEVEN_DAY,
    NOW_SHOWING,
    ALL_TIMESLOTS
}

// Working on the assumption that your int value is 
// the ordinal value of the items in your enum
public void onClick(DialogInterface dialog, int which) {
    // do your own bounds checking
    GuideView whichView = GuideView.values()[which];
    switch (whichView) {
        case SEVEN_DAY:
            ...
            break;
        case NOW_SHOWING:
            ...
            break;
    }
}

你可能会发现它更有用/更容易出错写一个自定义的 valueOf 实现,将整数值作为参数来解析适当的枚举值,并允许您集中边界检查。

You may find it more helpful / less error prone to write a custom valueOf implementation that takes your integer values as an argument to resolve the appropriate enum value and lets you centralize your bounds checking.

这篇关于Java使用带有switch语句的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆