Enum.valueOf(String)方法来自哪里? [英] Where does the Enum.valueOf(String) method come from?

查看:1689
本文介绍了Enum.valueOf(String)方法来自哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java SE 7中(最有可能是以前的版本),Enum类声明如下:

  public abstract class枚举< E extends Enum< E> 
extends Object
实现可比较的< E>,可序列化

Enum类具有此签名的静态方法:

  T static< T extends Enum< T> valueOf(Class< T> enumType,String name)

但没有静态方法: valueOf(String)在Enum类中定义或在Enum所属的层次结构中向上。



问题是 valueOf(String)来自哪里?
是否是语言的一个特性,即在编译器中构建的特性?

解决方案


请注意,对于特定的枚举类型T,可以使用该枚举上的隐式声明的公共静态T valueOf(String)方法而不是该方法来从名称映射到相应的枚举常量。枚举类型的所有常量可以通过调用该类型的隐式public static T [] values()方法获得。


Java语言规范,第8.9.2节< a>:


此外,如果E是枚举类型的名称,那么该类型具有以下隐式声明的静态方法: / p>



  / ** 
*返回包含此枚举常量的数组
*类型,按它们声明的顺序。这个方法可以是
*,用于遍历常量,如下所示:
*
* for(E c:E.values())
* System.out.println C);
*
* @按照声明的顺序返回一个包含这个枚举的常量的数组
* type
* /
public static E [] values ();

/ **
*返回具有指定的
*名称的此类型的枚举常量。
*字符串必须与在此类型中声明
*枚举常量的标识符完全匹配。 (不允许有多余的空格
*字符)
*
* @返回带有指定名称的枚举常量
* @throws IllegalArgumentException如果此枚举类型没有
*具有指定名称的常量
* /
public static E valueOf(String name);


In Java SE 7 (and most probably in previous versions) the Enum class is declared like this:

 public abstract class Enum<E extends Enum<E>>
 extends Object
 implements Comparable<E>, Serializable

The Enum class has a static method with this signature:

  T static<T extends Enum<T>> valueOf(Class<T> enumType, String name) 

But there is no static method : valueOf(String) defined in the Enum class nor upwards in the hierarchy Enum belongs to.

The question is where does valueOf(String) come from ? Is it a feature of the language, i.e. a feature built in the compiler ?

解决方案

This method is implicitly defined by the compiler.

From the documentation:

Note that for a particular enum type T, the implicitly declared public static T valueOf(String) method on that enum may be used instead of this method to map from a name to the corresponding enum constant. All the constants of an enum type can be obtained by calling the implicit public static T[] values() method of that type.

From the Java Language Specification, section 8.9.2:

In addition, if E is the name of an enum type, then that type has the following implicitly declared static methods:

/**
* Returns an array containing the constants of this enum 
* type, in the order they're declared.  This method may be
* used to iterate over the constants as follows:
*
*    for(E c : E.values())
*        System.out.println(c);
*
* @return an array containing the constants of this enum 
* type, in the order they're declared
*/
public static E[] values();

/**
* Returns the enum constant of this type with the specified
* name.
* The string must match exactly an identifier used to declare
* an enum constant in this type.  (Extraneous whitespace 
* characters are not permitted.)
* 
* @return the enum constant with the specified name
* @throws IllegalArgumentException if this enum type has no
* constant with the specified name
*/
public static E valueOf(String name);

这篇关于Enum.valueOf(String)方法来自哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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