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
});
}
}
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
});
}
}
相关文章推荐
- (jfinal)数据库添加内容后,自动生成model下的Java文件方法
- 自动活动的应用配置界面,将java方法设置添加到技术手段设置里,自动活动执行报错
- res中添加的资源不能在R.java中自动生成的原因及解决方法
- Java中 每个类在没有声明构造方法的前提下,会自动生成一个不带参数的构造方法
- Java中Enum类下的values()方法的由来
- Java命名规范及给自己写的方法添加注解
- java 继承 方法重写 自动转型 多态 接口 抽象类
- 编译器根据属性自动添加实例变量
- java基础总结 --- enum枚举的常量相关方法、自动售货机(状态模式)例子
- 三种获得自动生成主键的方法,getGeneratedKeys,专用SQL和可更新的结果集
- Java添加事件监听的四种方法代码实例
- java字符串截取、替换、删除、添加、查询等常用方法
- java 动态代理(模式) InvocationHandler(为类中方法执行前或后添加内容)
- 整理用Java实现数字转化成字符串左边自动补零方法
- JAVA进阶之旅(一)——增强for循环,基本数据类型的自动拆箱与装箱,享元设计模式,枚举的概述,枚举的应用,枚举的构造方法,枚举的抽象方法
- Java应用技巧:SE6调用编译器的两种方法(1)
- java字符串处理类----以后会继续添加方法到里面
- PHP 下载文件时如何自动添加bom头及解释BOM头和去掉bom头的方法
- Android4.0-4.4 添加实体按键振动支持的方法(java + smali版本)
- a3d7 java中给集合添加一组元素的几种方法