回顾一下多例设计模式:
范例:定义一个描述颜色基色的多例设计类
class Color {
private static final Color RED = new Color("红") ;
private static final Color YELLOW = new Color("黄") ;
private static final Color BLUE = new Color("蓝") ;
private String title ;
private Color(String title) {
this.title = title ;
}
public static Color getInstance(int ch) {
switch (ch) {
case 0 : return RED ;
case 1 : return YELLOW ;
case 2 : return BLUE ;
default : return null ;
}
}
public String toString() {
return this.title ;
}
}
public class TestDemo {
public static void main(String[] args) {
System.out.println(Color.getInstance(0)) ;
}
}
这样做的好处是:限制了本类实例化对象的产生个数。但是从JDK1.5开始引入了枚举,所以可以用一个简单程序代替上述代码:
范例:基于枚举开发
enum Color {
RED , YELLOW , BLUE ;
}
public class TestDemo {
public static void main(String[] args) {
System.out.println(Color.RED) ;
}
}
可以看出,枚举就是高级的多例设计模式。
使用enum定义的一个枚举类就相当于使用class定义的一个普通类继承了java.lang.Enum父类。
在Enum类中有如下方法:
方法名 | 类型 | 功能 |
---|---|---|
protected Enum(String name, int ordinal) | 构造 | 当定义枚举类的对象时自动设置序号和名字 |
public final String name() | 普通 | 取得枚举名 |
public final int ordinal() | 普通 | 取得枚举序号 |
values() | 普通 | 取得所有的枚举数据,返回一个对象数组。 |
范例:观察方法的使用
public static void main(String[] args) {
System.out.println(Color.RED.ordinal() + " = " + Color.RED.name()) ;
System.out.println(Color.BLUE.ordinal() + " = " + Color.BLUE.name()) ;
}
public static void main(String[] args) {
for (Color temp : Color.values()) {
System.out.println(temp.ordinal() + " = " + temp.name()) ;
}
}
虽然枚举等同于多例设计,但是多例设计是在一个类中产生的,所以在该类中可以定义更多的属性或者方法。
枚举如果只依靠以上的概念只能说产生了若干个对象,但是并没有方法去定义更多的结构,所以在枚举设计时考虑到了这些因素,提出了:
范例:在枚举中定义方法
enum Color {
// 类中没有无参构造,枚举对象定义好参数
RED("红色") , YELLOW("黄色") , BLUE("蓝色") ; // 如果定义有多种结构,枚举对象必须写在第一行
private String title ;
private Color(String title) { // 枚举(多例设计)原则:构造不能是public
this.title = title ;
}
public String toString() { // 覆写Object类中的方法
return this.title ;
}
}
public class TestDemo {
public static void main(String[] args) {
System.out.println(Color.RED) ;
}
}
范例:在枚举中实现接口
这时枚举中每一个对象实际上都变成了接口对象。
interface IColor {
public String getColor() ;
}
enum ColorImpl implements IColor{
// 类中没有无参构造,枚举对象定义好参数
RED("红色") , YELLOW("黄色") , BLUE("蓝色") ; // 如果定义有多种结构,枚举对象必须写在第一行
private String title ;
private ColorImpl(String title) { // 枚举(多例设计)原则:构造不能是public
this.title = title ;
}
public String toString() { // 覆写Object类中的方法
return this.title ;
}
@Override
public String getColor() {
return this.title ;
}
}
public class TestDemo {
public static void main(String[] args) {
IColor c = ColorImpl.RED ;
System.out.println(c.getColor()) ;
}
}
枚举最大的特点是:只有指定的几个对象可以使用。
范例:定义一个表示性别的枚举类
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 ;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
enum Sex{
// 类中没有无参构造,枚举对象定义好参数
MALE("男") , FEMALE("女") ; // 如果定义有多种结构,枚举对象必须写在第一行
private String title ;
private Sex(String title) { // 枚举(多例设计)原则:构造不能是public
this.title = title ;
}
public String toString() { // 覆写Object类中的方法
return this.title ;
}
}
public class TestDemo {
public static void main(String[] args) {
Person per = new Person("dexter" , 20 , Sex.MALE);
System.out.println(per) ;
}
}
枚举本身还支持switch判断。
enum Sex{
MALE , FEMALE ;
}
public class TestDemo {
public static void main(String[] args) {
switch(Sex.FEMALE) {
case MALE : System.out.println("男") ;
break ;
case FEMALE : System.out.println("女") ;
break ;
}
}
}