单独的枚举文件将不接受变量 [英] Separate Enum file won't accept variables

查看:53
本文介绍了单独的枚举文件将不接受变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了 enum.java 文件,创建变量时出错。但是在我的其他.java文件中,这些错误都没有出现。在枚举Foo中,唯一不会引起错误的是Foo构造函数不带任何参数并且枚举内没有其他变量。

I've created an enum.java file and I get errors when creating variables. But in my other .java files, none of these errors appear. Within the enum Foo, the only thing causes no errors is if the Foo constructor takes no parameters and thatthere are no other variables within the enum.

错误的范围是String

The errors range from the String being an invalid modifier and the boolean to be deleted.

package com.foo.bar

public enum Foo
{
    String foo;
    boolean isBarable;

    Foo(String foo, boolean isBarable)
    {
        this.foo = foo;
        this.isBarable = isBarable;
    }
}


推荐答案

您缺少枚举的最重要元素:枚举实例。

You're missing the most important element of the enum: the enum instances.

public enum Foo
{
    //  instances go here

    ;   // **** semicolon needed

    private String foo;
    private boolean isBarable;

    private Foo(String foo, boolean isBarable)
    {
        this.foo = foo;
        this.isBarable = isBarable;
    }
}

拍摄,仅添加分号即可解决编译问题错误,但没有枚举实例,则枚举是无用的。

Shoot, just adding the semicolon alone would solve your compilation error, but without the enum instances, the enum is useless.

例如,

public enum Foo
{
    BAR("bar", true), BAZ("baz", false) ;

    private String foo;
    private boolean isBarable;

    private Foo(String foo, boolean isBarable)
    {
        this.foo = foo;
        this.isBarable = isBarable;
    }
}

这篇关于单独的枚举文件将不接受变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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