您的位置:首页 > 编程语言 > Java开发

Java的编译器给enum自动添加了哪些专用方法?

2010-04-02 16:10 471 查看
from Java Tutorial:

Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. 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.

请问:
除values()方法外,编译器还给这个enum添加了哪些方法?

简单例子 :
enum Test{
Spring,Summer,Autumn,Winter
}
反编译的结果:
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: Test.java

final class Test extends Enum
{

public static final Test[] values()
{
return (Test[])$VALUES.clone();
}

public static Test valueOf(String s)
{
return (Test)Enum.valueOf(Test, s);
}

private Test(String s, int i)
{
super(s, i);
}

public static final Test Spring;
public static final Test Summer;
public static final Test Autumn;
public static final Test Winter;
private static final Test $VALUES[];

static
{
Spring = new Test("Spring", 0);
Summer = new Test("Summer", 1);
Autumn = new Test("Autumn", 2);
Winter = new Test("Winter", 3);
$VALUES = (new Test[] {
Spring, Summer, Autumn, Winter
});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐