Java `Enum.name()` 和 `Enum.toString()` 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18031125/
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
What is the difference between `Enum.name()` and `Enum.toString()`?
提问by Micha Wiedenmann
After reading the documentation of String java.lang.Enum.name()
I am not sure I understand when to use name()and when to use toString().
在阅读了String java.lang.Enum.name()
我不确定何时使用name()以及何时使用toString()的文档后。
Returns the name of this enum constant, exactly as declared in its enum declaration. Most programmers should use the toString method in preference to this one, as the toString method may return a more user-friendly name. This method is designed primarily for use in specialized situations where correctness depends on getting the exact name, which will not vary from release to release.
返回此枚举常量的名称,与在其枚举声明中声明的完全相同。大多数程序员应该优先使用 toString 方法而不是这个方法,因为 toString 方法可能会返回一个更用户友好的名称。此方法主要设计用于特定情况,其中正确性取决于获取确切名称,该名称不会因版本而异。
In particular, even though the documentation says to prefer toString()
, Java's own StandardLocationenumeration uses name
when I would have thought the documentation suggested otherwise.
特别是,即使文档说更喜欢toString()
,Java 自己的StandardLocation枚举name
在我认为文档另有建议时使用。
public String getName() { return name(); }
Furthermore Enum
implements toString()
as,
此外Enum
实现toString()
为,
public String toString() {
return name;
}
and I cannot think of a situation where a user defined enumeration would overwrite toString()
so name()
and toString()
are almost always exactly the same.
我想不出用户定义的枚举会覆盖toString()
如此name()
并且toString()
几乎总是完全相同的情况。
- Can you please explain why ignoring the documentation and always using
name()
is a bad idea? - What about the phrase "will not vary from release to release". If name will not vary, does it imply that
java.lang.Enum.toString()
would?
- 你能解释一下为什么忽略文档并总是使用
name()
是一个坏主意吗? - “不会因版本而异”这句话怎么样?如果名称不会变化,是否意味着
java.lang.Enum.toString()
会变化?
采纳答案by Hans Brende
The main difference between name()
and toString()
is that name()
is a final
method, so it cannot be overridden. The toString()
method returns the same value that name()
does by default, but toString()
can be overridden by subclasses of Enum.
name()
和之间的主要区别在于toString()
它name()
是一种final
方法,因此不能被覆盖。该toString()
方法返回的值name()
与默认情况下相同,但toString()
可以被 Enum 的子类覆盖。
Therefore, if you need the name of the field itself, use name()
. If you need a string representation of the value of the field, use toString()
.
因此,如果您需要字段本身的名称,请使用name()
. 如果需要字段值的字符串表示形式,请使用toString()
.
For instance:
例如:
public enum WeekDay {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY;
public String toString() {
return name().charAt(0) + name().substring(1).toLowerCase();
}
}
In this example,
WeekDay.MONDAY.name()
returns "MONDAY", and
WeekDay.MONDAY.toString()
returns "Monday".
在本例中,
WeekDay.MONDAY.name()
返回“MONDAY”,并
WeekDay.MONDAY.toString()
返回“Monday”。
WeekDay.valueOf(WeekDay.MONDAY.name())
returns WeekDay.MONDAY
, but WeekDay.valueOf(WeekDay.MONDAY.toString())
throws an IllegalArgumentException
.
WeekDay.valueOf(WeekDay.MONDAY.name())
返回WeekDay.MONDAY
,但WeekDay.valueOf(WeekDay.MONDAY.toString())
抛出一个IllegalArgumentException
。
回答by user541686
Use toString
when you need to display the name to the user.
使用toString
时需要显示的名字给用户。
Use name
when you need the name for your programitself, e.g. to identify and differentiate between different enum values.
name
当您需要程序本身的名称时使用,例如识别和区分不同的枚举值。
回答by Butani Vijay
Use toString()when you want to present information to a user (including a developer looking at a log). Never rely in your code on toString()
giving a specific value. Never test it against a specific string. If your code breaks when someone correctly changes the toString()
return, then it was already broken.
当您想向用户(包括查看日志的开发人员)显示信息时,请使用toString()。永远不要依赖你的代码toString()
给出一个特定的值。切勿针对特定字符串对其进行测试。如果您的代码在有人正确更改toString()
返回值时中断,那么它已经损坏了。
If you need to get the exact name used to declare the enum constant, you should use name()as toString
may have been overridden.
如果您需要获得用于声明枚举常量的确切名称,您应该使用name() ,因为它toString
可能已被覆盖。