2016-02-17 45 views
2

我完全不熟悉Java。我有一个接口,有几个我需要实现的方法。在界面内部,有一个类需要访问我需要的枚举。访问接口内的枚举

它看起来像这样:

public interface Operations{ 
    //some function names that I have to implement 
    public static enum ErrorCodes{ 
     BADFD; 
     NOFILE; 
     ISDIR; 
     private ErrorCode{ 
     } 
    } 
} 

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

编辑:添加大写的“o”,以枚举名

+1

我认为这是在你的代码一个错字错误。枚举名称是ErrorCodes,但您试图调用ErrorCode.BADFD。尝试调用ErrorCodes.BADFD –

+1

'static enum'和'private'构造函数是多余的,因为所有内部枚举类型都是隐式静态的,并且枚举构造函数也是隐式私有的。 – Pshemo

+0

你的'enum'构造函数中有另一个错字。 'private ErrorCode {'应该是'private ErrorCode(){' –

回答

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

要引用的接口ErrorCode之外,你必须用ErrorCode的封闭限定它接口,Operations

Operations.ErrorCode code = Operations.ErrorCode.BADFD; 
+2

或者''ErrorCode.BADFD',如果你导入'Operations.ErrorCode'。 ---或者'BADFD'如果*静态*导入'Operations.ErrorCode.BADFD'。 – Andreas

1

我相信你必须调用枚举这样:

Operations.ErrorCode.BADFD

因为错误代码是操作界面的内部枚举。

我注意到几个错字的问题,看看下面的代码:

public interface Operations { 
    //some function names that I have to implement 
    public static enum ErrorCode { 
     BADFD, 
     NOFILE, 
     ISDIR; 
     private ErrorCode() { 
     } 
    } 
} 
2

以下是更正一个

public interface Operations{ 
//some function names that I have to implement 
public static enum ErrorCodes{ 
    BADFD, 
    NOFILE, 
    ISDIR; 
    private ErrorCodes(){} 
}