java - 为什么这个枚举类型没有正确解析?

标签 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

最佳答案

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

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

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

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

关于java - 为什么这个枚举类型没有正确解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15729716/

相关文章:

java - 如何在 SwingX 中使用相对单元格突出显示?

java - 如何在JAVA Swing中实现如下屏幕/布局

c# - 您在数据应用程序中使用枚举吗?如何使用?

java - 基于字符串内容的枚举 valueof

swift - 如何在 switch 语句之外访问 Swift 枚举关联值

c# - 将 RadioButton 绑定(bind)到枚举

java - 在 Java 中使用单个 for-each 循环遍历多个列表

java - 如何使用 lambda 表达式在 Java 的一行中创建值为 0,1,2,...,N 的 int[] 或 Collection<Integer>?

java - 何时使用 pathParams 或 QueryParams

javascript - 根据条件 ng-change 更改 ng-model 字段的值