如何返回一个枚举数组,遇到此错误:

huangapple go评论91阅读模式
英文:

How to return an array of enums, get this error

问题

public enum colors{
    绿色,
    黄色,
    红色,
    错误;
    
    public static colors[] values(){
        /*
         返回一个包含此枚举类型常量的数组,按照它们被声明的顺序排列。
        */
        colors[] c = {绿色,黄色,红色,错误};
        return c;
    }
}

获取错误:values() 在 colors 中已经被定义。

英文:
public enum colors{
Green,
YELLOW,
RED,
ERROR;
   public static colors[] values(){
/*
 Returns an array containing the constants of this enum type, in the order they are declared.
*/
   colors[] c = {GREEN,YELLOW,RED,ERROR};
   return c;
   }
}

get the error: values() is already defined in colors

答案1

得分: 1

这种方法是由编译器隐式定义的,因此如果您尝试在枚举中再次声明此方法,您将会得到编译错误,类似于"The enum <class-name>.colors already defines the method values() implicitly"

您可以在这里查看文档,https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9.2

请注意上述文档中的这一行,

"由此可见,枚举类型声明不能包含与枚举常量冲突的字段,并且不能包含与自动生成的方法(values() 和 valueOf(String))冲突的方法,或者包含覆盖 Enum 中的 final 方法的方法(equals(Object)、hashCode()、clone()、compareTo(Object)、name()、ordinal() 和 getDeclaringClass())"

在这里,E 是枚举类型的名称,那么该类型具有以下隐式声明的静态方法。

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

因此,您无需再次声明它,而是可以通过简单地调用colors.values()来获取数组。

例如,请参考以下简单的代码片段:

public class Test {
   
	public static void main(String[] args) {
		colors[] values = colors.values();
		System.out.println(Arrays.toString(values));
	}
	
	public enum colors {
		Green,
		YELLOW,
		RED,
		ERROR;
	}	
}

输出:

[Green, YELLOW, RED, ERROR]
英文:

This method is implicitly defined by the compiler, so if you will try to declare this method again into your enum, you will get compile error like &quot;The enum &lt;class-name&gt;.colors already defines the method values() implicitly&quot;

You can check the documentation here, https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9.2

Note this line in the above documentation,

"It follows that enum type declarations cannot contain fields that conflict with the enum constants, and cannot contain methods that conflict with the automatically generated methods (values() and valueOf(String)) or methods that override the final methods in Enum (equals(Object), hashCode(), clone(), compareTo(Object), name(), ordinal(), and getDeclaringClass())."

Here, 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&#39;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&#39;re declared
*/
public static E[] values();

Therefore you do not need to declare it again, instead you can get array by simply calling colors.values().

For an Example, refer the below simple code snippet:

public class Test {
   
	public static void main(String[] args) {
		colors[] values = colors.values();
		System.out.println(Arrays.toString(values));
	}
	
	public enum colors {
		Green,
		YELLOW,
		RED,
		ERROR;
	}	
}

OUTPUT:

[Green, YELLOW, RED, ERROR]

答案2

得分: 1

在Java中,枚举会生成一个名为values()的静态方法,它不能被重写,正如Java规范中所指定的那样。

英文:

Enums in java will have a generated static method values(), and it cannot be overriden , as specified in the Java Spec.

答案3

得分: 0

请查阅文档

编译器在创建枚举时会自动添加一些特殊方法。例如,它们有一个静态的values方法,该方法返回一个数组,其中包含按照声明顺序排列的枚举的所有值。这个方法通常与for-each结构一起使用,以便遍历枚举类型的值。例如,下面来自Planet类示例的代码遍历了太阳系中的所有行星。

英文:

Please check the documentation

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. For example, this code from the Planet class example below iterates over all the planets in the solar system.

huangapple
  • 本文由 发表于 2020年3月16日 20:55:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/60706419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定