JAVA枚举使用

在数学和计算机科学理论中,一个集的枚举是列出某些 有穷序列集所有 成员的程序,或者是一种特定类型对象的计数。

多例设计模式

在介绍枚举之前,先介绍多例设计模式。

多例设计模式的特点是构造函数私有化,类内部需要提供若干个实例化对象

这是JDK1.5之前的做法,这样做的目的是:限制本类实例化对象产生的个数。但是,从JDK1.5开始有了枚举,可以使代码更加精简。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* 多例设计模式
*/
class Color {
public static final Color RED = new Color("RED");
public static final Color GREEN = new Color("GREEN");
public static final Color BLUE = new Color("BLUE");
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 GREEN;
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).toString());
}
}

枚举

使用示例

1
2
3
4
5
6
7
8
9
enum Color {
RED,GREEN,BLUE;
}
public class TestDemo {
public static void main(String[] args) {
System.out.println(Color.RED);
}
}

其实,枚举就是一种高级的多例设计模式

获取序号和名字

1
2
3
4
5
6
7
8
9
enum Color {
RED,GREEN,BLUE;
}
public class TestDemo {
public static void main(String[] args) {
System.out.println(Color.RED.ordinal() + " = " + Color.RED.name());
}
}

使用values()获取枚举类里实例的数组

1
2
3
4
5
6
7
8
9
10
11
enum Color {
RED,GREEN,BLUE;
}
public class TestDemo {
public static void main(String[] args) {
for (Color temp : Color.values()) {
System.out.println(temp.ordinal() + " = " + temp.name());
}
}
}

enum和Enum的区别

enum是一个关键字,使用enum定义的枚举类本质上就相当于一个类继承了Enum抽象类

在枚举中定义其他结构

定义属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
enum Color {
// 枚举类中如果有很多内容,枚举对象必须写在第一行
RED("红色"),GREEN("绿色"),BLUE("蓝色");
private String title;
// 构造方法绝对不能使用public修饰符
private Color(String title) {
this.title = title;
}
// 重写Object类中toString()方法
public String toString() {
return this.title;
}
}
public class TestDemo {
public static void main(String[] args) {
System.out.println(Color.RED);
}
}

实现接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
interface IColor {
public String getColor();
}
enum Color implements IColor {
// 枚举类中如果有很多内容,枚举对象必须写在第一行
RED("红色"),GREEN("绿色"),BLUE("蓝色");
private String title;
// 构造方法绝对不能使用public修饰符
private Color(String title) {
this.title = title;
}
// 重写Object类中toString()方法
public String toString() {
return this.title;
}
@Override
public String getColor() {
return this.title;
}
}
public class TestDemo {
public static void main(String[] args) {
IColor ic = Color.GREEN;
System.out.println(ic.getColor());
}
}

实例应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Person{
private String name;
private int age;
private Gender gender;
public Person(String name, int age, Gender gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
'}';
}
}
enum Gender {
MALE("男"),FEMALE("女");
private String title;
private Gender(String title) {
this.title = title;
}
public String toString() {
return title;
}
}
public class TestDemo {
public static void main(String[] args) {
Person p = new Person("zhangsan",22,Gender.MALE);
System.out.println(p.toString());
}
}

其他

switch判断,最初只支持int、char;自从JDK1.5开始,支持枚举;自从JDK1.7开始,支持String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
enum Gender {
MALE, FEMALE;
}
public class TestDemo {
public static void main(String[] args) {
Gender g = Gender.MALE;
switch (g) {
case FEMALE:
System.out.println("是女人");
break;
case MALE:
System.out.println("是男人");
break;
default:
System.out.println("不确定");
}
}
}
If you think the content is useful to you.