Java 使用 enum 和 switch 语句
我查看了与此问题类似的各种问答,但尚未找到解决方案。
我拥有的是一个枚举,它代表观看电视指南的不同方式...
在 NDroid Application
类中
static enum guideView {
GUIDE_VIEW_SEVEN_DAY,
GUIDE_VIEW_NOW_SHOWING,
GUIDE_VIEW_ALL_TIMESLOTS
}
...当用户更改视图时,事件处理程序会收到一个 int 从 0-2 ,我想做这样的事情...
在 Android Activity
onClick(DialogInterfacedialog, int which)
事件处理程序中,
// 'which' is an int from 0-2
switch (which) {
case NDroid.guideView.GUIDE_VIEW_SEVEN_DAY:
...
break;
}
我用于 C# 枚举和 select/case 语句,这将允许类似上面我知道 Java 的做法不同,但我就是不明白我需要做什么。
我是否必须求助于 if
语句?可能只有 3 个选择,所以我可以做到这一点,但我想知道如何使用 Java 中的 switch-case 来完成它。
编辑 抱歉,我没有完全扩展这个问题,因为我将其视为一个通用的 Java 问题。我已添加到问题中以进一步解释。
没有任何特定于 Android 的内容,这就是为什么我没有将其标记为 Android,但枚举是在 Application
类中定义的,并且我不希望切换的代码位于 活动
。枚举是静态的,因为我需要从多个活动访问它。
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...
In the NDroid Application
class
static enum guideView {
GUIDE_VIEW_SEVEN_DAY,
GUIDE_VIEW_NOW_SHOWING,
GUIDE_VIEW_ALL_TIMESLOTS
}
...when the user changes the view an event handler receives an int
from 0-2 and I'd like to do something like this...
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;
}
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.
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.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您缺少的部分是将整数转换为类型安全枚举。 Java 不会自动执行此操作。有几种方法可以解决此问题:
guideView.GUIDE_VIEW_SEVEN_DAY.ordinal()
确定int值表示的枚举值,然后开启枚举值。
您可能会发现编写自定义
valueOf
实现更有帮助/更不容易出错,该实现将整数值作为参数来解析适当的枚举值,并让您集中边界检查。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:
guideView.GUIDE_VIEW_SEVEN_DAY.ordinal()
Determine the enum value represented by the int value and then switch on the enum value.
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.如果
whichView
是 GuideView 枚举的对象,则以下效果很好。请注意,case
之后的常量没有限定符。If
whichView
is an object of the GuideView Enum, following works well. Please note that there is no qualifier for the constant aftercase
.枚举不应像
NDroid.guideView.GUIDE_VIEW_SEVEN_DAY
那样在 case 标签内进行限定,而是应删除限定并使用GUIDE_VIEW_SEVEN_DAY
The enums should not be qualified within the case label like what you have
NDroid.guideView.GUIDE_VIEW_SEVEN_DAY
, instead you should remove the qualification and useGUIDE_VIEW_SEVEN_DAY
我喜欢 Java 枚举的一些用法:
具有值参数的枚举:
开关示例:
I like a few usages of Java enum:
enum with value parameters:
switch example:
简短的关联函数示例:
就像@Dhanushka所说,省略“switch”内的限定符是关键。
Short associative function example:
Like @Dhanushka said, omit the qualifier inside "switch" is the key.
这应该按照您描述的方式工作。你遇到什么错误?如果你可以粘贴你的代码会有帮助。
http://download.oracle.com/javase/tutorial/java/javaOO /enum.html
编辑:您确定要定义静态枚举吗?这对我来说听起来不对。枚举与任何其他对象非常相似。如果您的代码编译并运行但给出了不正确的结果,这可能就是原因。
This should work in the way that you describe. What error are you getting? If you could pastebin your code that would help.
http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
EDIT: Are you sure you want to define a static enum? That doesn't sound right to me. An enum is much like any other object. If your code compiles and runs but gives incorrect results, this would probably be why.
我这样做就像
在 Switch 语句中使用一样
I am doing it like
And use in Switch Statement