java Enum.valueOf(String) 方法从何而来?

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

Where does the Enum.valueOf(String) method come from?

javacompiler-constructionenumsvalue-of

提问by Razvan

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

在 Java SE 7(并且很可能在以前的版本中)中,Enum 类声明如下:

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

The Enum class has a static method with this signature:

Enum 类有一个带有此签名的静态方法:

  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.

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

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

问题是valueOf(String)从哪里来?它是语言的特性,即编译器中内置的特性吗?

采纳答案by dasblinkenlight

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.

请注意,对于特定的枚举类型 T,可以使用该枚举上隐式声明的 public static T valueOf(String) 方法代替此方法将名称映射到相应的枚举常量。枚举类型的所有常量都可以通过调用该类型的隐式 public static T[] values() 方法获得。

From the Java Language Specification, section 8.9.2:

来自Java 语言规范的第 8.9.2 节

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

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

/**
* 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);

回答by The Coordinator

I think it must be a feature of the language. For one, to create an enum by making one and it does not need to extend Enum:

我认为这一定是语言的一个特征。一方面,通过创建一个枚举来创建一个枚举,并且不需要扩展枚举:

public enum myEnum { red, blue, green }

That alone, is a language feature or else you would need to do this:

仅此一项就是语言功能,否则您需要这样做:

public class  MyEnum extends Enum { ..... }

Secondly, the method, Enum.valueOf(Class<T> enumType, String name), must be produced by the compiler when you you use myEnum.valueOf(String name).

其次,Enum.valueOf(Class<T> enumType, String name)当您使用myEnum.valueOf(String name).

That is what would seem possible given that the new Enumis as much a language feature as it is a class to extend.

鉴于 newEnum既是一个语言特性又是一个要扩展的类,这似乎是可能的。