Java 枚举和 Switch 语句 - 默认情况? [英] Java Enums and Switch Statements - the default case?

查看:37
本文介绍了Java 枚举和 Switch 语句 - 默认情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于建议抛出异常的人:
抛出异常不会给我一个编译时错误,它会给我一个运行时错误.我知道我可以抛出异常,我宁愿在编译期间死也不愿在运行时死.

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)没有认识到这个开关涵盖了所有可能性(或者它是否涵盖了它们?)并停止警告我需要返回类型.有没有一种方法可以在不向 Mode 中添加方法的情况下做我想做的事?

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 语句发出警告/错误?

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

然后您将实现如下内容:

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

}

您可以按如下方式使用它:

You'd use it as follows:

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 枚举和 Switch 语句 - 默认情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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