“意外类型”比较枚举值时出错

发布于 12-02 19:56 字数 763 浏览 5 评论 0原文

我见过的大多数遇到此问题的人都在需要 == 的地方使用 =。是什么导致了我这里的问题?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ㄟ。诗瑗2024-12-09 19:56:56

使用静态访问而不是实例访问:

if (event.getType() == DataLayoutEvent.Type.INSERT) {

您可以(但不应该)对静态成员(方法和字段)使用实例访问,但不能对内部类型使用实例访问。

Use static access instead of instance access:

if (event.getType() == DataLayoutEvent.Type.INSERT) {

You can (but shouldn't) use instance access for static members (methods and fields), but not for inner types.

半步萧音过轻尘2024-12-09 19:56:56

它应该只是:

// From within DataLayoutEvent
if (event.getType() == Type.INSERT) {

// From other classes
if (event.getType() == DataLayoutEvent.Type.INSERT) {

Type 部分是类型的名称 - 它不能由变量值(event)限定。顺便说一句,如果您想使用其他地方的第一个表单,您可以导入 DataLayoutEvent.Type

It should just be:

// From within DataLayoutEvent
if (event.getType() == Type.INSERT) {

or

// From other classes
if (event.getType() == DataLayoutEvent.Type.INSERT) {

The Type part is the name of a type - it can't be qualified by a variable value (event). You could import DataLayoutEvent.Type if you wanted to use the first form from elsewhere, by the way.

携君以终年2024-12-09 19:56:56

我认为您需要以不同的方式引用 Type

if (event.getType() == DataLayoutEvent.Type.INSERT) { ... }

I think you need to refer to Type differently:

if (event.getType() == DataLayoutEvent.Type.INSERT) { ... }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文