为什么这个枚举类型没有正确解决?

时间:2013-03-31 13:13:52

标签: java enums

以下java代码无法在eclipse中编译。我在这做错了什么?如果方法只返回int而不是enum,那么一切正常,所以它基本上设置好了。问题在于引入枚举返回类型。

public class myclass {
  public enum mytype {
      mytype2,
      mytype1,  
  };
  public static mytype retmytype() {
    return mytype2;
  }
}

//in another class
myclass.mytype t = myclass.retmytype(); //ERROR - myclass.mytype cannot be solved

1 个答案:

答案 0 :(得分:1)

尝试将return mytype2;替换为return mytype.mytype2;

顺便说一下,你应该遵循Java命名约定;)

我认为您忘记了一个主方法(或程序流程中的任何其他调用方法)。 试试这个:

public class myclass {
  public enum mytype {
      mytype2,
      mytype1,  
  };
  public static mytype retmytype() {
    return mytype.mytype2;
  }
  public static void main(String[] args){
    myclass.mytype t = myclass.retmytype();
  }
}