Java枚举enum的用法
来源:互联网 发布:淘宝旧版本下载 编辑:程序博客网 时间:2024/05/22 03:03
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<com.example.enumtest.Suit> { 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<Suit> set = EnumSet.allOf(Suit.class); for (Suit s : set) { System.out.println(s); } System.out.println("---"); EnumSet<Suit> range = EnumSet.range(Suit.DIAMONDS, Suit.HEARTS); for (Suit s : range) { System.out.println(s); } }}打印结果:
CLUBSDIAMONDSHEARTSSPADES---DIAMONDSHEARTS
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<Suit, String> map = new EnumMap<Suit, String>(Suit.class); map.put(Suit.CLUBS, "梅花牌"); map.put(Suit.DIAMONDS, "方块牌"); map.put(Suit.HEARTS, "红桃牌"); map.put(Suit.SPADES, "黑桃牌"); Set<Map.Entry<Suit, String>> entries = map.entrySet(); Iterator<Map.Entry<Suit, String>> iterator = entries.iterator(); while (iterator.hasNext()) { Map.Entry<Suit, String> 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<E> getDeclaringClass()
* public final int hashCode()
* public final String name()
* public final int ordinal()
* public String toString()
enum类隐式继承了Enum,Enum类继承自Object,并且实现了Comparable<E>,Serializable
public abstract class Enum<E extends Enum<E>> extends Object implements Comparable<E>, Serializable
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
- Java enum枚举的用法
- java enum枚举的用法
- Java枚举enum的用法
- 【java】枚举enum用法
- java中枚举类型Enum的用法
- Java Enum(枚举一)的用法
- Java 枚举类Enum的用法总结
- Java 枚举(Enum)的用法
- Java 枚举(enum)的用法
- 枚举 Enum 的用法
- enum(枚举)的用法
- 枚举 的用法 enum
- 枚举enum的用法
- JAVA枚举ENUM用法详解
- Java Enum 枚举用法详解
- Java 枚举(enum) 常见用法
- java的枚举enum
- enum枚举类型的用法
- JQuery中ajax,get方法在asmx中的使用
- shadowsocks一键安装
- poj1364 King(差分约束)
- 69、java集合-TreeMap
- SpringBoot学习笔记(十):解决跨域问题
- Java枚举enum的用法
- NULL and nullptr
- 人工智能投资真热还是“虚火”? 投资仍是小众行为
- matlab运行出现错误For floats all values in I must be smaller than 1.解决方法
- shell正则匹配日期
- 使用Java读取xlxs文件和写入txt文件,并将数据写入到本地文件
- JS面向(基于)对象编程笔记
- 70、集合工具类-Collections
- PLSQL Developer V9 注册码