Enum的values()方法的文档在哪里? [英] Where is the documentation for the values() method of Enum?

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

问题描述

我声明一个枚举:

 枚举性别{MALE,FEMALE}; 

然后,如下所示迭代枚举:



(Sex v:Sex.values()){
System.out.println(values:+ v);

  
}

我检查了Java API,但找不到values()方法?我很好奇这个方法来自哪里?



API链接:
https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html

解决方案

您不能在javadoc中看到此方法,因为它由编译器添加。



记录在三个位置: / p>




当创建
一个枚举时,编译器会自动添加一些特殊方法。例如,它们具有静态值方法,返回一个
数组,其中包含枚举的所有值,它们是
声明的顺序。这种方法通常与
结合使用,每个构造体迭代枚举类型的值。





  • Enum.valueOf class
    (特殊隐式方法在$ code> valueOf 方法的描述中提到)




枚举类型的所有常量可以通过调用该类型的隐式public static T [] values()方法获得。






函数只是列出所有的价值枚举。


I declare an enum as :

enum Sex {MALE,FEMALE};

And then, iterate enum as shown below :

for(Sex v : Sex.values()){
    System.out.println(" values :"+ v);
}

I checked the Java API but can't find the values() method? I'm curious as to where this method comes from?

API link : https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html

解决方案

You can't see this method in javadoc because it's added by the compiler.

Documented in three places :

The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type.

  • Enum.valueOf class
    (The special implicit values method is mentioned in description of valueOf method)

All the constants of an enum type can be obtained by calling the implicit public static T[] values() method of that type.

The values function simply list all values of the enumeration.

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

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