我应该如何将这些数据存储在 Java 枚举中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9914959/
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-10-30 22:52:53  来源:igfitidea点击:

How should I store this data in a Java enum?

javaenums

提问by Webnet

What's the best way to store this data in a Java enum?

将此数据存储在 Java 枚举中的最佳方法是什么?

<select>
    <option></option>
    <option>Recommend eDelivery</option>
    <option>Require eDelivery</option>
    <option>Require eDelivery unless justification provided</option>
</select>

I'm new to java and have tried things like

我是 Java 新手,并尝试过类似的事情

public enum Paperless { 
      "None" = null,
      "Recommend eDelivery" = "Recommend eDelivery",
      "Require eDelivery" = "Require eDelivery",
      "Require eDelivery unless justification provided" = "Require eDelivery w/out justification"
}

But this doesn't work. I'm considering the possibility of storing a text value that summarizes the option that the user sees on this web page.

但这不起作用。我正在考虑存储一个文本值的可能性,该文本值总结了用户在此网页上看到的选项。

回答by Robin

Take a look at the enum tutorial, more specifically the Planetexample. You can do the same, e.g.

查看enum 教程,更具体地说是Planet示例。你也可以这样做,例如

public enum Paperless{
  NONE( null ),
  RECOMMENDED_DELIVERY( "Recommended delivery" ),
  ...//put here the other values
  REQUIRED_DELIVERY( "Required delivery" );
  private String name;
  Paperless( String name ){
    this.name = name;
  }
  public String getName(){
    return this.name;
  }
}

回答by jhenderson2099

You can't assign strings to enum values in Java in the way that you are trying.

您不能以您尝试的方式将字符串分配给 Java 中的枚举值。

The way to do it would be:

这样做的方法是:

public enum Paperless { 
      None(null), 
      RecommendedDelivery("Recommended Delivery"), 
      RequireEDelivery("Require eDelivery"), 
      RequireEDeliveryUnlessJustification("Require eDelivery unless justification provided");

      private final String value;   

      Paperless(String value) {
        this.value = value;
      }

      private String enumValue() { return value; }

      public static void main(String[] args) {
        for (Paperless p : Paperless.values())
           System.out.println("Enum:" + p + "; Value:" + p.enumValue());
      }
}

回答by anubhava

Something like this can work for your case:

这样的事情可以适用于您的情况:

public enum PaperLess {
    NONE("none"),
    RECOMMEND("Recommend eDelivery"),
    REQUIRE("Require eDelivery"),
    REQUIRE_JUSTIFIED("Require eDelivery unless justification provided");

    private String value;

    private PaperLess(String value) {
       this.value = value;
    }

    public String getValue() {
       return value;
    }
}

回答by A Null Pointer

    public enum Paperless {
        NONE("None"),
        RECOMMEND("Recommend eDelivery"),
        REQUIRE("Require eDelivery"),
        REQUIRE_UNLESS("Require eDelivery unless justification provided"),;

     private String value;
     private Paperless(String value){
         this.value=value;
     }

     public String getValue(){
         return this.value;
     }

   }

回答by GetSet

Java enums aren't constructed in that way. Check out

Java 枚举不是以这种方式构造的。查看

Java Tutorials: Enum Types

Java 教程:枚举类型

Java - Convert String to enum: #2

Java - 将字符串转换为枚举:#2

Yours might look something like this:

你的可能看起来像这样:

public enum Paperless {
  NONE(""),
  RECOMMEND("Recommend eDelivery"),
  REQUIRE("Require eDelivery"),
  REQUIRE_UNLESS("Require eDelivery unless justification provided");

  private String text;

  Paperless(String text) {
    this.text = text;
  }

  public String getText() {
    return this.text;
  }
}

回答by Peter Lawrey

You can't have spaces in the names of members and you can't assign enum values, they are objects, not constants.

成员名称中不能有空格,也不能分配枚举值,它们是对象,而不是常量。

回答by Attila

The name of the enum must be an identifier (e.g. one-word, not a string)

枚举的名称必须是一个标识符(例如一个单词,而不是一个字符串)

public enum Paperless {
  None,
  RecommendEDelivery,
  ...
}

You can associate string values with them if you want (although you can get the default too that equals to the identifier name, usign the name()method) by associating a String member with the enum type and providing a custom constructor.

如果需要,您可以将字符串值与它们相关联(尽管您也可以获得等于标识符名称的默认值,使用该name()方法),方法是将 String 成员与枚举类型相关联并提供自定义构造函数。

public enum Paperless {
  None("None"),
  RecommendEDelivery("Recommend eDelivery"),
  ...;

  private String myValue;

  private Paperless(String name) {myValue=name;)
}

To access that associated string, you need to provide a public accessor method as well.

要访问该关联字符串,您还需要提供一个公共访问器方法。