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添加了哪些方法?
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: [url]http://www.kpdus.com/jad.html[/url]
// 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
});
}
}
enum Test{
Spring,Summer,Autumn,Winter
}
反编译的结果:
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: [url]http://www.kpdus.com/jad.html[/url]
// 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
});
}
}
本文深入探讨Java编程语言中枚举类型的强大特性及其用法。介绍了编译器为枚举类型自动生成的方法,如values()和valueOf()等,并通过具体示例展示了枚举类型的内部实现细节。
8135

被折叠的 条评论
为什么被折叠?



