JDK 1.5三大主要新特性 —— 枚举
枚举的基本概念
在讲解枚举之前来回顾一个概念:多例设计模式,构造方法私有化(非public),之后在类的内部存在若干个指定的对象,通过一个方法返回指定对象。
|
package cn.mldn.demo; class Color { private static final Color RED = new Color("红色") ; private static final Color GREEN = new Color("绿色") ; private static final Color BLUE = new Color("蓝色") ; private String title ; private Color(String title) { this.title = title ; } public String toString() { return this.title ; } public static Color getInstance(int ch) { switch(ch) { case 0 : return RED ; case 1 : return GREEN ; case 2 : return BLUE ; default : return null ; } } } public class TestDemo { public static void main(String[] args) throws Exception { Color c = Color.getInstance(0) ; System.out.println(c); } } |
实际上在JDK 1.5之后对于多例设计模式有了一些新的改进,使用了一个新的关键字来进行定义 —— 枚举(就是一个简化了的多例设计模式),枚举使用enum关键字来进行定义。
|
package cn.mldn.demo; enum Color { RED,GREEN,BLUE ; } public class TestDemo { public static void main(String[] args) throws Exception { Color c = Color.RED ; System.out.println(c); } } |
很明显,现在可以发现,利用枚举实现的多例设计模式会更加的简单直白一些,但是在Java之中,枚举并不是一个新的类型,严格来讲,每一个使用enum定义的类实际上都属于一个类继承了Enum父类而已,而java.lang.Enum类定义:
|
public abstract class Enum<E extends Enum<E>> extends Object implements Comparable<E>, Serializable |
而在Enum类之中定义了两个方法:
· 取得枚举的序号:public final int ordinal();
· 取得枚举的名称:public final String name()。
范例:验证以上的两个方法
|
package cn.mldn.demo; enum Color { RED,GREEN,BLUE ; } public class TestDemo { public static void main(String[] args) throws Exception { for (Color c : Color.values()) { System.out.println(c.ordinal() + "," + c.name()); } } } |
每一个枚举对象都是根据其定义的顺序进行编号的,而且需要提醒的是,在JDK 1.5之后,switch可以接收枚举类型的判断。
|
package cn.mldn.demo; enum Color { RED,GREEN,BLUE ; } public class TestDemo { public static void main(String[] args) throws Exception { Color c = Color.RED ; switch(c) { case RED: System.out.println("红色"); break ; case GREEN : System.out.println("绿色"); break ; case BLUE : System.out.println("蓝色"); break ; } } } |
总结:关于switch允许的操作类型
· 在JDK 1.5之前,switch只能够操作int或char型数据;
· 在JDK 1.5之后,switch可以操作enum型;
· 在JDK 1.7之后,switch可以操作String型。
面试题:请解释一下enum和Enum的关系?
enum是JDK 1.5之后定义的新关键字,主要用于定义枚举类型,在Java之中每一个使用enum定义的枚举类型实际上都表示一个类默认继承了Enum类而已。
3.5.2 、枚举的其他定义
按照之前所理解,枚举就属于多例设计模式,那么既然是多例设计模式,对于类之中就肯定有多种组成,包括属性、方法、构造方法,在枚举之中也同样可以定义以上的内容,不过需要注意的是,枚举类之中定义的构造方法绝对不能是public,必须私有化。除了这些要求之外,枚举之中的每一个定义的对象,必须写在第一行。
|
package cn.mldn.demo; enum Color { RED("红色"),GREEN("绿色"),BLUE("蓝色") ; private String title ; private Color(String title) { this.title = title ; } public String toString() { return this.title ; } } public class TestDemo { public static void main(String[] args) throws Exception { Color c = Color.RED ; System.out.println(c); } } |
可是除了以上的定义之外,在枚举之中也可以实现接口。
范例:让枚举实现接口
|
package cn.mldn.demo; interface Message{ public String getColor() ; } enum Color implements Message { RED("红色"),GREEN("绿色"),BLUE("蓝色") ; private String title ; private Color(String title) { this.title = title ; } public String toString() { return this.title ; } public String getColor() { return this.toString(); } } public class TestDemo { public static void main(String[] args) throws Exception { Message msg = Color.RED ; System.out.println(msg.getColor()); } } |
而且枚举本身还有一些更为特殊的应用,例如:可以在枚举之中直接定义抽象方法,不过这个时候就要求枚举之中的每一个枚举对象分别实现这个抽象方法。
范例:在枚举中定义抽象方法
|
package cn.mldn.demo; enum Color { RED("红色") { public String getColor() { return this.toString(); } },GREEN("绿色"){ public String getColor() { return this.toString(); } },BLUE("蓝色"){ public String getColor() { return this.toString(); } } ; private String title ; private Color(String title) { this.title = title ; } public String toString() { return this.title ; } public abstract String getColor() ; // 抽象方法 } public class TestDemo { public static void main(String[] args) throws Exception { System.out.println(Color.RED.getColor()); } } |
那么枚举这个技术到底在开发之中有什么用呢?
范例:枚举的应用(好不容易才想出来的,唯一可以用的地方)
|
package cn.mldn.demo; enum Sex{ MALE("男"),FEMALE("女") ; private String title ; private Sex(String title) { this.title = title ; } public String toString() { return this.title ; } } class Person { private String name ; private int age ; private Sex sex ; public Person(String name,int age,Sex sex) { this.name = name ; this.age = age ; this.sex = sex ; } public String toString() { return "姓名:" + this.name + ",年龄:" + this.age + ",性别:" + this.sex ; } } public class TestDemo { public static void main(String[] args) throws Exception { Person per = new Person("张三",20,Sex.MALE) ; System.out.println(per); } } |
本程序不使用枚举照样可以实现,所以对于开发而言,如果已经习惯于使用枚举的可以继续使用,如果压根就没习惯用的,那么就别用了,之所以出现枚举,是因为大部分语言都有枚举,而java是在JDK 1.5之后才引入了枚举这一概念,基本上没什么用。
浙公网安备 33010602011771号