JAVA 枚举详解

来源:互联网 发布:我想学习编程 编辑:程序博客网 时间:2024/06/06 02:47

先看这一段代码:

enum AccountType{    SAVING, FIXED, CURRENT;    private AccountType()    {        System.out.println(“It is a account type”);    }}class EnumOne{    public static void main(String[]args)    {        System.out.println(AccountType.FIXED);    }}

输出为:
It is a account type
It is a account type
It is a account type
FIXED

解析:
枚举类在后台实现时,实际上是转化为一个继承了java.lang.Enum类的实体类,原先的枚举类型变成对应的实体类型,
上例中AccountType变成了个class AccountType,并且会生成一个新的构造函数,
若原来有构造函数,则在此基础上添加两个参数,生成新的构造函数,如上例子中:
1
privateAccountType(){ System.out.println(“It is a account type”); }
会变成:
1
2
privateAccountType(String s, inti){
    super(s,i); System.out.println(“It is a account type”); }
而在这个类中,会添加若干字段来代表具体的枚举类型:
1
2
3
publicstaticfinalAccountType SAVING;
publicstaticfinalAccountType FIXED;
publicstaticfinalAccountType CURRENT;
而且还会添加一段static代码段:
1
2
3
4
5
6
static{
    SAVING = newAccountType("SAVING"0);
    ...  CURRENT = newAccountType("CURRENT"0);
   $VALUES = newAccountType[]{
         SAVING, FIXED, CURRENT
    } }
以此来初始化枚举中的每个具体类型。(并将所有具体类型放到一个$VALUE数组中,以便用序号访问具体类型)
在初始化过程中new AccountType构造函数被调用了三次,所以Enum中定义的构造函数中的打印代码被执行了3遍。


0 0
原创粉丝点击