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

查看:28
本文介绍了`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 枚举使用 name 当我认为文档另有建议时.

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

此外,EnumtoString() 实现为,

Furthermore Enum implements toString() as,

public String toString() {
    return name;
}

而且我想不出用户定义的枚举会覆盖 toString() 的情况,所以 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() 可以被 Enum 的子类覆盖.

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.

WeekDay.valueOf(WeekDay.MONDAY.name()) returns WeekDay.MONDAY, but WeekDay.valueOf(WeekDay.MONDAY.toString()) throws an IllegalArgumentException.

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

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