“意外类型”比较枚举值时出错
我见过的大多数遇到此问题的人都在需要 ==
的地方使用 =
。是什么导致了我这里的问题?
com\callmeyer\jopp\FieldCoordinator.java:303: unexpected type required: class, package found : variable if (event.getType() == event.Type.INSERT) { ^
枚举定义和访问器:
public class DataLayoutEvent {
public static enum Type { INSERT, DELETE, RENAME, MOVE, RESIZE }
private Type type = null;
public Type getType() {
return type;
}
...
}
以及发生错误的方法:
public void layoutChanged(DataLayoutEvent event) {
if (event.getType() == event.Type.INSERT) {
fieldAdded(event.getField(), event.getToIndex());
}
...
Most people I've seen with this problem were using =
where they needed ==
. What's causing my problem here?
com\callmeyer\jopp\FieldCoordinator.java:303: unexpected type required: class, package found : variable if (event.getType() == event.Type.INSERT) { ^
The enum definition and accessor:
public class DataLayoutEvent {
public static enum Type { INSERT, DELETE, RENAME, MOVE, RESIZE }
private Type type = null;
public Type getType() {
return type;
}
...
}
and the method where the error occurs:
public void layoutChanged(DataLayoutEvent event) {
if (event.getType() == event.Type.INSERT) {
fieldAdded(event.getField(), event.getToIndex());
}
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用静态访问而不是实例访问:
您可以(但不应该)对静态成员(方法和字段)使用实例访问,但不能对内部类型使用实例访问。
Use static access instead of instance access:
You can (but shouldn't) use instance access for static members (methods and fields), but not for inner types.
它应该只是:
或
Type
部分是类型的名称 - 它不能由变量值(event
)限定。顺便说一句,如果您想使用其他地方的第一个表单,您可以导入DataLayoutEvent.Type
。It should just be:
or
The
Type
part is the name of a type - it can't be qualified by a variable value (event
). You could importDataLayoutEvent.Type
if you wanted to use the first form from elsewhere, by the way.我认为您需要以不同的方式引用
Type
:I think you need to refer to
Type
differently: