“Enum.name()”和“Enum.toString()”之间有什么区别? [英] What is the difference between `Enum.name()` and `Enum.toString()`?

查看:113
本文介绍了“Enum.name()”和“Enum.toString()”之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读的文档后String java.lang.Enum.name()我不知道我明白什么时候使用 name()以及何时使用 toString()

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().


返回此枚举常量的名称,与其枚举声明中声明的完全相同。大多数程序员应该优先使用toString方法,因为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(),Java自己的 StandardLocation 枚举使用名称,当我以为文档建议不同。 p>

In particular, even though the documentation says to prefer toString(), Java's own StandardLocation enumeration uses name when I would have thought the documentation suggested otherwise.

public String getName() { return name(); }

此外枚举实现 toString() as,

public String toString() {
    return name;
}

我无法想到用户定义的枚举会覆盖 toString() so name() toString()几乎总是完全一样的。

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.


  1. 您能否解释为什么忽略文档,并始终使用 name()是一个坏主意?

  2. 短语发布到发行版不会有所不同。如果名字不会变化,这是否意味着 java.lang.Enum.toString()会吗?

  1. Can you please explain why ignoring the documentation and always using name() is a bad idea?
  2. 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() toString() name()是一个 final 方法,所以不能被覆盖。 toString()方法默认返回与 name()相同的值,但 toString()可以被枚举的子类覆盖。

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

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().

例如:

public enum WeekDay {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY;

    public String toString() {
        return name().charAt(0) + name().substring(1).toLowerCase();
    }
}

在这个例子中,
WeekDay.MONDAY.name()返回MONDAY,
WeekDay.MONDAY.toString()返回星期一

In this example, WeekDay.MONDAY.name() returns "MONDAY", and WeekDay.MONDAY.toString() returns "Monday".

WeekDay.valueOf(WeekDay.MONDAY.name())返回 WeekDay .MONDAY ,但 WeekDay.valueOf(WeekDay.MONDAY.toString())抛出一个 IllegalArgumentException

这篇关于“Enum.name()”和“Enum.toString()”之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆