Java1.5增加了enum类型
1. 创建枚举类型时,必须用enum关键字。所有创建的枚举类型都隐式继承了java.lang.Enum,不可以使用定义一个类继承Enum类的方法来定义枚举类型。
public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES
}
2. 上面的代码中构造函数缺省,默认会映射到Enum(String name, int ordinal)构造函数中。
枚举类型可以使用参数定义自己的构造函数。枚举类型的构造函数都是默认为
private的。枚举类型的实例定义(CLUBS等)在变量定义(type)之前。
public enum Suit {
CLUBS(1),
DIAMONDS(2),
HEARTS(3),
SPADES(4);
private int type;
Suit(int type) {
this.type = type;
}
}
3. 枚举类型里定义的每一个值都是枚举类型的一个实例,
每个实例都是隐式的public static final的,即唯一、静态且不可修改。不可以在实例前加public static final修饰符。
使用javac命令将上面的Suit.java进行编译生成Suit.class文件,紧接着使用javap命令将Suit.class文件进行反编译,如下
DavidtekiMacBook-Air:desktop du$ javap Suit.class
Compiled from "Suit.java"
public final class com.example.enumtest.Suit extends java.lang.Enum {
public static final com.example.enumtest.Suit CLUBS;
public static final com.example.enumtest.Suit DIAMONDS;
public static final com.example.enumtest.Suit HEARTS;
public static final com.example.enumtest.Suit SPADES;
public static com.example.enumtest.Suit[] values();
public static com.example.enumtest.Suit valueOf(java.lang.String);
static {};
}
反编译后,可以看出
①Suit是一个class且继承了Enum类,即所有创建的枚举类型都隐式继承了java.lang.Enum
②所有的实例都是public static final的
4. 枚举类型可以作为Switch的判断对象,这里要注意在case后面的枚举类型的值只需要写实例的名字即可(case CLUBS:),不能加枚举类型的类名,否则编译器报错,正确代码如下:
public enum Suit {
CLUBS(1),
DIAMONDS(2),
HEARTS(3),
SPADES(4);
private int type;
Suit(int type) {
this.type = type;
}
public static void main(String args[]) {
Suit clubs = Suit.CLUBS;
switch (clubs) {
case CLUBS:
System.out.println("clubs " + CLUBS.type);
break;
case DIAMONDS:
System.out.println("diamonds " + DIAMONDS.type);
break;
case HEARTS:
System.out.println("diamonds " + HEARTS.type);
break;
case SPADES:
System.out.println("diamonds " + SPADES.type);
break;
}
}
}
5. 枚举类型可以定义自己的方法
public enum Suit {
CLUBS(1),
DIAMONDS(2),
HEARTS(3),
SPADES(4);
private int type;
Suit(int type) {
this.type = type;
}
public int getType() {//定义自己的方法
return type;
}
}
6. 枚举类型的实例可以重写枚举类中的方法
public enum Suit {
CLUBS(1){
@Override
public int getType() {//重写
return 999;
}
},
DIAMONDS(2),
HEARTS(3),
SPADES(4);
private int type;
Suit(int type) {
this.type = type;
}
public int getType() {
return type;
}
}
7. 枚举类中可以定义抽象方法,此时所有的实例都必须实现这个抽象方法
public enum Suit {
CLUBS(1){
@Override
public int getType() {
return 999;
}
},
DIAMONDS(2){
@Override
int getType() {
return 0;
}
},
HEARTS(3) {
@Override
int getType() {
return 0;
}
},
SPADES(4) {
@Override
int getType() {
return 0;
}
};
private int type;
Suit(int type) {
this.type = type;
}
abstract int getType();
}
8. EnumSet的使用
public enum Suit {
CLUBS(1),
DIAMONDS(2),
HEARTS(3),
SPADES(4);
private int type;
Suit(int type) {
this.type = type;
}
public static void main(String args[]) {
EnumSet set = EnumSet.allOf(Suit.class);
for (Suit s : set) {
System.out.println(s);
}
System.out.println("---");
EnumSet range = EnumSet.range(Suit.DIAMONDS, Suit.HEARTS);
for (Suit s : range) {
System.out.println(s);
}
}
}
打印结果:
CLUBS
DIAMONDS
HEARTS
SPADES
---
DIAMONDS
HEARTS
9. EnumMap的使用
public enum Suit {
CLUBS(1),
DIAMONDS(2),
HEARTS(3),
SPADES(4);
private int type;
Suit(int type) {
this.type = type;
}
public static void main(String args[]) {
EnumMap map = new EnumMap(Suit.class);
map.put(Suit.CLUBS, "梅花牌");
map.put(Suit.DIAMONDS, "方块牌");
map.put(Suit.HEARTS, "红桃牌");
map.put(Suit.SPADES, "黑桃牌");
Set> entries = map.entrySet();
Iterator> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry next = iterator.next();
System.out.println(next.getKey() + " " + next.getValue());
}
}
}
打印结果:
CLUBS 梅花牌
DIAMONDS 方块牌
HEARTS 红桃牌
SPADES 黑桃牌
10. enum类的values方法和valueOf方法
public enum Suit {
CLUBS(1),
DIAMONDS(2),
HEARTS(3),
SPADES(4);
private int type;
Suit(int type) {
this.type = type;
}
public static void main(String args[]) {
Suit[] suits = Suit.values();//返回包含所有实例的数组
for (Suit s : suits) {
System.out.println(s);
}
Suit clubs = Suit.valueOf("CLUBS");//返回对应的实例,不存在则报错No enum constant
System.out.println(clubs.type);
}
}
11. enum对象的预定义方法
* public final Object clone() throws CloneNotSupportedException
* public final int compareTo(E e)
* public final boolean equals(Object o)
* public final Class
* public final int hashCode()
* public final String name()
* public final int ordinal()
* public String toString()
enum类隐式继承了Enum,Enum类继承自Object,并且实现了Comparable
public abstract class Enum
public enum Suit {
CLUBS(1),
DIAMONDS(2),
HEARTS(3),
SPADES(4);
private int type;
Suit(int type) {
this.type = type;
}
public static void main(String args[]) {
Suit clubs = Suit.CLUBS;
System.out.println(clubs.compareTo(Suit.SPADES));//-3 注:0减去3
System.out.println(clubs.equals(Suit.DIAMONDS));//false
System.out.println(clubs.hashCode());//1360875712 注:不同的人不一样的值
System.out.println(clubs.getDeclaringClass());//class com.example.enumtext.Suit
System.out.println(clubs.name());//CLUBS
System.out.println(clubs.toString());//CLUBS
System.out.println(clubs.ordinal());//0
try {
System.out.println(clubs.clone());
} catch (CloneNotSupportedException e) {//抛出CloneNotSupportedException异常
e.printStackTrace();
}
}
}
参考链接:
http://www.cnblogs.com/hyl8218/p/5088287.html
https://zhidao.baidu.com/question/40640070
http://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html#clone--
Java语言规范下载(现在不能0积分上传=.=表示没办法):
http://download.csdn.net/download/kikitious_du/10015957