如何在java中返回枚举值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31849497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:41:21  来源:igfitidea点击:

How to return enum value in java

javaenumsreturn

提问by Mathew

How can I return enums like this?

我怎样才能返回这样的枚举?

Before I was returing an int with 0 if no, 1 if yes and 2 if other. But this wasn't good way to do. So how should it be done. My code:

在我返回一个 int 之前,如果不是,则为 0,如果是,则为 1,如果其他,则为 2。但这可不是什么好办法。那么应该怎么做。我的代码:

class SomeClass{
   public enum decizion{
      YES, NO, OTHER
   }

   public static enum yourDecizion(){
      //scanner etc
      if(x.equals('Y')){
         return YES;
      }
      else if (x.equals('N')){
         return NO;
      }
      else{
         return OTHER;
      }
   }
}

采纳答案by Konstantin Yovkov

I don't what the "//scanner etc." does, but the methods return type should be decizion:

我不知道“//扫描仪等” 确实如此,但方法返回类型应该是decizion

public static decizion yourDecizion() { ... }

Furthermore, you can add the Y, N, etc. values to the enum constants:

此外,您可以将YN等值添加到枚举常量中:

public enum decizion{
     YES("Y"), NO("N"), OTHER;

     String key;

     decizion(String key) { this.key = key; }

     //default constructor, used only for the OTHER case, 
     //because OTHER doesn't need a key to be associated with. 
     decizion() { }

     decizion getValue(String x) {
         if ("Y".equals(x)) { return YES; }
         else if ("N".equals(x)) { return NO; }
         else if (x == null) { return OTHER; }
         else throw new IllegalArgumentException();
     }
}

Then, in the method, you can just do:

然后,在该方法中,您可以执行以下操作:

public static decizion yourDecizion() {
    ...
   String key = ...
   return decizion.getValue(key);
}

回答by Axel

Change your code to:

将您的代码更改为:

class SomeClass{
   public enum decizion {
      YES, NO, OTHER
   }

   public static decizion yourDecizion(){
      //scanner etc
      if(x.equals('Y')){
         return decizion.YES;
      }
      else if (x.equals('N')){
         return decizion.NO;
      }
      else{
         return decizion.OTHER;
      }
   }
}

Note: The method return type must be decizioninstead of enumand decizionshould have an upper case name (as all classes should).

注意:方法返回类型必须是decizion而不是enum并且decizion应该有一个大写的名称(所有类都应该如此)。

回答by Eduardo

I think you should do something like these, an enum class. Then u can add as many types u want and the method yourDecizion() will return the enum type depending on the given parameter.

我认为你应该做这样的事情,一个枚举类。然后您可以添加任意数量的类型,并且 yourDecizion() 方法将根据给定的参数返回枚举类型。

public enum SomeClass {

        YES(0),
        NO(1),
        OTHER(2);

    private int code;


    private SomeClass(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    public static SomeClass yourDecizion(int x) {
        SomeClass ret = null;
        for (SomeClass type : SomeClass.values()) {
            if (type.getCode() == x)
                ret = type;
        }
        return ret;
    }
}