访问界面内的枚举

Neo*_*Neo 2 java enums interface

我是Java的新手.我有一个接口,有一些我需要实现的方法.在界面内部,有一个类我需要访问的枚举.

它看起来像这样:

public interface Operations{
    //some function names that I have to implement
    public static enum ErrorCodes{
        BADFD;
        NOFILE;
        ISDIR;
        private ErrorCode{
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的实现中,当我尝试访问ErrorCodes.BADFD它时给了我错误.我不知道访问它的正确方法.另外,什么是空调private ErrorCode{}.它是构造函数吗?它有什么作用?

编辑:在枚举名称中添加了大写的"o"

rge*_*man 7

首先,让我们纠正您的错误代码:

// lowercase "interface"
// Usually interfaces and classes are capitalized
public interface Operations{
    // Singular to match the rest of the code and question.
    public static enum ErrorCode{
        // commas to separate instances
        BADFD,
        NOFILE,
        ISDIR;
        // Parameterless constructor needs ()
        private ErrorCode() {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ErrorCode在接口外部引用,必须使用ErrorCode封闭接口限定它Operations.

Operations.ErrorCode code = Operations.ErrorCode.BADFD;
Run Code Online (Sandbox Code Playgroud)

  • 如果导入`Operations.ErrorCode`,则为`ErrorCode.BADFD`.---或者``BADFD`如果你*静态*导入`Operations.ErrorCode.BADFD`. (2认同)