Java枚举和切换语句 - 默认情况? [英] Java Enums and Switch Statements - the default case?
问题描述
对于建议抛出异常的人:
抛出异常并不会给我一个编译时错误,它给我一个运行时错误。我知道我可以抛出一个异常,我宁愿在编译期间比运行时死亡。
For people suggesting throwing an exception:
Throwing an exception doesn't give me a compile-time error, it gives me a runtime error. I know I can throw an exception, I'd rather die during compilation than during runtime.
首先,我使用eclipse 3.4。
First-off, I am using eclipse 3.4.
我有一个模式属性为枚举的数据模型。
I have a data model that has a mode property that is an Enum.
enum Mode {on(...), off(...), standby(...); ...}
我正在写这个模型的视图,我有代码
I am currently writing a view of this model and I have the code
...
switch(model.getMode()) {
case on:
return getOnColor();
case off:
return getOffColor();
case standby:
return getStandbyColor();
}
...
我收到错误此方法必须返回类型为java.awt.Color的结果,因为我没有默认情况,在函数结尾没有返回xxx。
我希望在有人向枚举添加其他类型(例如关闭)的情况下的编译错误,因此我不想放置抛出AssertionError的默认情况,因为这将使用修改的模式进行编译,直到运行时才会被视为错误。
I am getting an error "This method must return a result of type java.awt.Color" because I have no default case and no return xxx at the end of the function. I want a compilation error in the case where someone adds another type to the enum (e.g. shuttingdown) so I don't want to put a default case that throws an AssertionError, as this will compile with a modified Mode and not be seen as an error until runtime.
我的问题是这样的:
为什么EclipseBuilder(和javac)是否不认识到此开关涵盖所有可能性(或覆盖它们?),并停止警告我需要返回类型。有没有办法,我可以做我想要的,而不添加方式到模式?
My question is this:
Why does EclipseBuilder (and javac) not recognize that this switch covers all possibilities (or does it cover them?) and stop warning me about needing a return type. Is there a way I can do what I want without adding methods to Mode?
没有,是否有一个选项来警告/错误的switch语句不覆盖所有Enum的可能值?
Failing that, is there an option to warn/error on switch statements that don't cover all of the Enum's possible values?
编辑:
Rob:这是一个编译错误。我只是试图用javac编译它,并且我得到一个针对最后一个方法的缺少的返回语句错误。 Eclispe只是将错误放在方法的顶部。
Rob: It is a compile error. I just tried compiling it with javac and I get a "missing return statement" error targeting the last } of the method. Eclispe just places the error at the top of the method.
推荐答案
您可以随时使用枚举与访问者模式:
You could always use the Enum with Visitor pattern:
enum Mode {
on {
public <E> E accept( ModeVisitor<E> visitor ) {
return visitor.visitOn();
}
},
off {
public <E> E accept( ModeVisitor<E> visitor ) {
return visitor.visitOff();
}
},
standby {
public <E> E accept( ModeVisitor<E> visitor ) {
return visitor.visitStandby();
}
}
public abstract <E> E accept( ModeVisitor<E> visitor );
public interface ModeVisitor<E> {
E visitOn();
E visitOff();
E visitStandby();
}
}
然后你会实现如下所示: p>
Then you would implement something like the following:
public final class ModeColorVisitor implements ModeVisitor<Color> {
public Color visitOn() {
return getOnColor();
}
public Color visitOff() {
return getOffColor();
}
public Color visitStandby() {
return getStandbyColor();
}
}
您将使用如下:
return model.getMode().accept( new ModeColorVisitor() );
这是一个更详细的,但是如果一个新的枚举被声明,你会立即得到一个编译错误。
This is a lot more verbose but you'd immediately get a compile error if a new enum was declared.
这篇关于Java枚举和切换语句 - 默认情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!