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
It is a account type
It is a account type
FIXED
解析:
枚举类在后台实现时,实际上是转化为一个继承了java.lang.Enum类的实体类,原先的枚举类型变成对应的实体类型,
上例中AccountType变成了个class AccountType,并且会生成一个新的构造函数,
若原来有构造函数,则在此基础上添加两个参数,生成新的构造函数,如上例子中:
1
private
AccountType(){ System.out.println(“It is a account type”); }
会变成:
1
2
private
AccountType(String s,
int
i){
super
(s,i); System.out.println(“It is a account type”); }
1
2
3
public
static
final
AccountType SAVING;
public
static
final
AccountType FIXED;
public
static
final
AccountType CURRENT;
而且还会添加一段static代码段:
1
2
3
4
5
6
static
{
SAVING =
new
AccountType(
"SAVING"
,
0
);
... CURRENT =
new
AccountType(
"CURRENT"
,
0
);
$VALUES =
new
AccountType[]{
SAVING, FIXED, CURRENT
} }
在初始化过程中new AccountType构造函数被调用了三次,所以Enum中定义的构造函数中的打印代码被执行了3遍。
0 0
- java枚举使用详解
- java枚举使用详解
- java枚举使用详解
- java 枚举用法详解
- JAVA 枚举详解
- java枚举使用详解
- Java枚举的详解
- java 枚举用法详解
- java枚举使用详解
- java枚举详解实例
- java枚举使用详解
- Java枚举类详解
- java 枚举详解
- java枚举使用详解
- java枚举使用详解
- java枚举使用详解
- java枚举使用详解
- java枚举使用详解
- Spring+Hibernate整合开发配置,完整实例
- bash脚本错误检查
- React(1)--开始学习
- Math类、Date、SimpleDateFormat
- xcode8 升级后部分电脑注释快捷键不能使用的解决方法
- JAVA 枚举详解
- Linux-系统管理-nat&实现跨网段跨运营商代理
- 最简单易懂的闭包
- xcode 8 一些新特性
- Hadoop回顾--MapReduce异常处理
- java 中Date的各种格式转换
- 测试ThreadPoolExecutor的一个demo1
- 多种方法实现字符串逆序
- 2016 ACM/ICPC Asia Regional Shenyang Online 1009 QSC and Master 区间dp