1、枚举的概念
多例设计:一个类只能产生有限多个实例化对象,那么枚举的功能实际上就属于这种功能的实现,JDK 1.5之后增加了新的关键字:enum。
范例:定义一个枚举类
enum Color {
RED, GREEN, BLUE;
}
public class EnumDemo01 {
public static void main(String args[]) {
Color c = Color.RED;
System.out.println(c);
}
};
2、enum关键字和Enum类
enum Color {
RED, GREEN, BLUE;
}
public class EnumDemo02 {
public static void main(String args[]) {
for (Color c : Color.values()) {
System.out.println(c.ordinal() + " --> " + c.name());
}
}
};
枚举通过enum关键字定义,定义的枚举就相当于一个类继承了Enum类。
3、定义属性及方法
使用enum定义了一个枚举之后也可以像普通类那样定义属性和方法,包括构造方法,但是一定要注意的是,所有的构造方法一定是private。否则报错 Illegal modifier for the enum constructor; only private is permitted.
enum Color {
RED("红色"), GREEN("绿色"), BLUE("蓝色");
private Color(String title) {
this.setTitle(title);
}
private String title;
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
}
public class EnumDemo03 {
public static void main(String args[]) {
for (Color c : Color.values()) {
System.out.println(c.ordinal() + " --> " + c.name() + " --> "
+ c.getTitle());
}
}
};
在一般的开发中完全可以通过类的设计来达到枚举的功能。
4、实现接口
枚举本身也可以实现接口,但是需要注意的是,一旦一个枚举实现了接口之后,枚举中的每个对象都必须分别的实例化这些接口中提供的抽像方法。
interface Info {
public String getColorInfo();
}
enum Color implements Info {
RED("红色") {
public String getColorInfo() {
return this.getTitle();
}
},
GREEN("绿色") {
public String getColorInfo() {
return this.getTitle();
}
},
BLUE("蓝色") {
public String getColorInfo() {
return this.getTitle();
}
};
private Color(String title) {
this.setTitle(title);
}
private String title;
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
}
public class EnumDemo04 {
public static void main(String args[]) {
for (Color c : Color.values()) {
System.out.println(c.ordinal() + " --> " + c.name() + " --> "
+ c.getColorInfo());
}
}
};
5、定义抽象方法
在枚举中还可以定义抽象方法,但是与实现接口一样的是,每一个枚举对象都要分别实现此方法。
enum Color implements Info {
RED("红色") {
public String getColorInfo() {
return this.getTitle();
}
},
GREEN("绿色") {
public String getColorInfo() {
return this.getTitle();
}
},
BLUE("蓝色") {
public String getColorInfo() {
return this.getTitle();
}
};
private Color(String title) {
this.setTitle(title);
}
private String title;
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
public abstract String getColorInfo();
}
public class EnumDemo05 {
public static void main(String args[]) {
for (Color c : Color.values()) {
System.out.println(c.ordinal() + " --> " + c.name() + " --> "
+ c.getColorInfo());
}
}
};
6、枚举的应用
枚举的最大特点实际上是限制了一个类的取值范围,例如:在设置习惯年别的时候,只能是男或女,那么此时应用枚举是最合适的地方。
enum Sex {
MALE("男"), FEMALE("女");
private String name;
private Sex(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
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 EnumDemo06 {
public static void main(String args[]) {
System.out.println(new Person("张三", 20, Sex.MALE));
}
};
但是通过其他的代码也可以满足此种要求,所以枚举在开发中是否使用并不是绝对的。
7、枚举与switch语句
switch语句,case分支条件后面只能是int,char或者枚举类型
public enum Animal {
dog,cat,pig;
static Animal getValue(String animal)
{
return valueOf(animal.toLowerCase());
}
}
switch
public class Client {
public void caseAnimal(String animal) {
switch(Animal.getValue(animal)) {
case dog:
System.out.println("thisis a dog");
break;
case pig:
System.out.println("thisis a pig ");
break;
case cat:
System.out.println("thisis a cat");
break;
default:
System.out.println("idon't know what is this!");
break;
}
}
public static void main(String[] args) {
new Client().caseAnimal("dog");
}
}