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

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

问题描述

我声明一个枚举为:

enum Sex {MALE,FEMALE};

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

And then, iterate enum as shown below :

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

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

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

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

推荐答案

你在javadoc中看不到这个方法,因为它是编译器添加的.

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

记录在三个地方:

  • Enum Types, from The Java Tutorials

编译器在创建的时候会自动添加一些特殊的方法一个枚举.例如,它们有一个静态值方法,该方法返回一个按顺序包含枚举的所有值的数组宣布.这种方法通常与for-each 构造来迭代枚举类型的值.

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
    (valueOfvalues方法> 方法)
  • 枚举类型的所有常量都可以通过调用该类型的隐式 public static T[] values() 方法获得.

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

    • 枚举类型,第 8.9 节, Java 语言规范
    • values 函数只是列出枚举的所有值.

      The values function simply list all values of the enumeration.

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

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