默认情况下将 Java ENUM 打印为小写,将枚举常量保持为大写

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

Print Java ENUM to lower case by default keeping enum constants in uppercase

javaenums

提问by spacebiker

I have an ENUM in JAVA i'd like to serialize, so that when i call it from anywhere in the code i get the lowercase representation of the name.

我在 JAVA 中有一个 ENUM 我想序列化,这样当我从代码中的任何地方调用它时,我都会得到名称的小写表示。

Let's say i have the following enum:

假设我有以下枚举:

public enum Status {
    DRAFT, PENDING, COMPLETE;
}
println ("Status=" + Status.DRAFT);

i'd like to get the following:

我想得到以下信息:

Status=draft

[Note]: I want to use the enum constants in uppercase, and when requesting the value get the lowercase representation.

[注意]:我想使用大写的枚举常量,并且在请求值时获取小写表示。

采纳答案by spacebiker

I am replying this question myself as i found the solution interesting and could not find a reply in the site. Just in case somebody else looks for a way to solve this.

我自己回答这个问题,因为我发现解决方案很有趣,但在网站上找不到回复。以防万一其他人寻找解决此问题的方法。

The solution is simple, just override the Enum toString method like this:

解决方案很简单,只需像这样覆盖 Enum toString 方法:

public enum Status {
    DRAFT, PENDING, COMPLETE;

    @Override
    public String toString() {
        return name().toLowerCase();
    }
}
println ("Status=" + Status.DRAFT);

This would output the name in lower case.

这将以小写形式输出名称。

回答by Peter Lawrey

If you want lower case, you could just use lower case, or mixed case, or whatever makes more sense to you.

如果你想要小写,你可以使用小写或混合大小写,或者任何对你更有意义的东西。

public enum Status {
    draft, pending, complete;
}

println ("Status=" + Status.draft);

prints

印刷

Status=draft

回答by Lorenzo Sciuto

Another solution could be:

另一种解决方案可能是:

public enum Status {
  DRAFT, PENDING, COMPLETE;

  public String nameLowerCase(){
        return name().toLowerCase();
    }
}

回答by Lucky

You can use the following Enum class which contains constructor with name and ordinal for each enum constant. You can assign values you need for the enum constant.

您可以使用以下 Enum 类,其中包含每个枚举常量的名称和序号的构造函数。您可以为枚举常量分配所需的值。

public enum Status {

DRAFT(0,"draft"), PENDING(1,"pending"), COMPLETE(2,"complete");

private int key;
private String value;

Status(int key, String value){
    this.key = key;
    this.value = value;
}

public int getKey() {
    return key;
}

public void setKey(int key) {
    this.key = key;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

@Override
public String toString(){
    return this.value;
}

}

Since we override the toStringmethod, the value which is lowercase is returned.

由于我们覆盖了该toString方法,因此返回小写的值。

Using

使用

System.out.print("Status = "+Status.DRAFT);

would print,

会打印,

Status = draft

and

System.out.print("Status = "+Status.DRAFT.name());

would print

会打印

Status = DRAFT