Java的编译器给enum自动添加了哪些专用方法?
来源:互联网 发布:php商城开源 编辑:程序博客网 时间:2024/06/05 04:21
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的编译器给enum自动添加了哪些专用方法?
- 给xmpphp添加了几个常用的方法
- enum类型添加自定义的描述方法
- java.lang.Enum的方法
- 给大家介绍一下很好的 C 语言专用编译器 —— Win-TC
- DEDECMS添加迅雷专用下载链接的方法
- java基础总结 --- enum枚举的常量相关方法、自动售货机(状态模式)例子
- Java跟踪到运行了哪些类哪些方法
- Java - 给编译器看的Annotation
- Java - 给编译器看的Annotation
- 【Java】给org.json 添加 json转Bean的方法
- C++编译器会自动生成哪些函数
- 被编译器给耍了
- java enum的应用(给自己写着玩的)
- 今天找反编译器找郁闷了,提供个JAVA反编译器下载地址给大家
- 给EasyUi的Form添加自动填充部分输入框的方法
- 给EasyUi的Form添加自动填充部分输入框的方法
- java用enum实现singleton的方法
- CString的GetLength()函数返回为0问题解决
- .net sql防注入代码
- 解决出现 ./configure and got “configure: error: C++ compiler cannot create executables的方法
- pku 2234 matches game 博弈
- 找零钱的代码
- Java的编译器给enum自动添加了哪些专用方法?
- Windows中ntfs和fat32的区别和选择
- POJ_1001
- JAVAAPPLET游戏碰撞代码
- 符号三角形问题;回溯算法;子集树问题;时间复杂度O(2的n次方);
- 论有效的需求分析过程
- RIA好莱坞之旅:将视频加入你的应用程序中
- EventDemo——演示事件流
- 【转贴】linux Shell编程入门 实例讲解详解