枚举是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类多了一些特殊的约束,但是这些约束的存在也使得枚举类型具有简洁、安全以及便捷的优良特性。枚举是一系列具有名称的常量的集合,比如:一年的 4 个季节,一个年的 12 个月份,一个星期的 7 天,方向的东南西北,性别的男女等。
一、枚举类型概述
传统常量在实际开发中习惯性的被定义在接口中。而Java的枚举类型也常被用于设置常量。
枚举类型是一种数据类型,被视为一系列具有名称的常量的集合,因此被赋予了在程序编译时检查数据类型的功能,使得枚举类型定义常量的方式逐渐取代传统的常量定义形式。
1、枚举的定义和使用:
(1)传统方式定义常量
public interface Constants{
public static final int Constants_A = 1;
public static final int Constants_B = 12;
}
(2)使用枚举定义常量
public enum Constants{
Constants_A,
Constants_B
}
- Constants:枚举类名
- Constants_A:常量1
- Constants_B:常量2
(3)枚举中常量的使用
使用 Constants_A常量:
Constants.Constants_A
实例1:使用枚举类定义常量 ,并使用枚举类中的常量
定义一个枚举类Color,枚举中包含Red、Yello、Blue、Green常量,分别表示红色、黄色、蓝色和绿色。
enum Color{
Red,
Yello,
Blue,
Green
}
public class Test{
public static void main(String[] args){
Color c1 = Color.Red;
Color c2 = Color.Green;
System.out.println(c1);
System.out.println(c2);
}
}
Console:
Red
Green
2、枚举类型的特点
- Java的枚举类型常被用于设置常量。
- 枚举类型被视为一系列具有名称的常量的集合,所以被赋予了在程序编译时检查数据类型的功能。
- Java 枚举类使用 enum 关键字来定义,各个常量使用逗号“,”来分割。
- 在使用枚举时,只能使用枚举中包含的值,其他的值无法使用。
二、枚举的使用
1、内部类中使用枚举
枚举类也可以声明在内部类中:
public class Test2 {
enum Color{
Red,
Yello,
Blue,
Green
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Color c1 = Color.Red;
Color c2 = Color.Green;
System.out.println(c1);
System.out.println(c2);
}
}
Console:
Red
Green
2、迭代枚举元素
可以使用 for 语句来迭代枚举元素:
enum Colors{
Red,
Yello,
Blue,
Green
}
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(Colors myColor:Colors.values()) {
System.out.println(myColor);
}
}
}
执行以上代码输出结果为:
Console:
Red
Yello
Blue
Green
3、在 switch 中使用枚举类
枚举类常应用于 switch 语句中:
enum Colors{
Red,
Yello,
Blue,
Green
}
public class Test4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Color myColor = Color.Red;
switch(myColor) {
case Red:
System.out.println("红色");
break;
case Yello:
System.out.println("黄色");
break;
case Blue:
System.out.println("蓝色");
break;
case Green:
System.out.println("绿色");
break;
}
}
}
红色
三、枚举类的常用方法
enum 定义的枚举类默认继承了 java.lang.Enum 类,并实现了 java.lang.Seriablizable 和 java.lang.Comparable 两个接口。
values(), ordinal() 和 valueOf() 方法位于 java.lang.Enum 类中:
- values() 返回枚举类中所有的值。
- ordinal()方法可以找到每个枚举常量的索引,就像数组索引一样。
- valueOf()方法返回指定字符串值的枚举常量。
enum Colors{
Red,
Yello,
Blue,
Green
}
public class Test5{
public static void main(String[] args) {
// TODO Auto-generated method stub
//调用values()方法
Colors[] arr = Colors.values();
//使用ordinary()方法查看索引值
for(Colors col:arr) {
System.out.println(col+"所在索引值是:"+col.ordinal());
}
//使用valuesOf()方法返回枚举常量
System.out.println(Colors.valueOf("Green"));
//System.out.println(Colors.valueOf("Black")); //不存在的值会报错 IllegalArgumentException*
}
}
Console:
Red所在索引值是:0
Yello所在索引值是:1
Blue所在索引值是:2
Green所在索引值是:3
Green
四、枚举类成员
枚举跟普通类一样可以用自己的变量、方法和构造函数,构造函数只能使用 private 访问修饰符,所以外部无法调用。
枚举既可以包含具体方法,也可以包含抽象方法。 如果枚举类具有抽象方法,则枚举类的每个实例都必须实现它。
enum Color3{
Red,
Yello,
Blue,
Green;
private Color3() {
System.out.println("Constructor called for : " + this.toString());
}
public void colorInfo() {
System.out.println("全部颜色!");
}
}
public class Test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Color3 c1 = Color3.Red;
System.out.println(c1);
c1.colorInfo();
}
}
Console:
Constructor called for : Red
Constructor called for : Yello
Constructor called for : Blue
Constructor called for : Green
Red
全部颜色!
五、传统常量与枚举类型常量的区别
-
枚举类型在程序编译时被赋予了检查数据类型的功能,而传统常量在编译时是不具备检查数据类型的功能的。
-
如果有一个参数以接口形式定义的方法,我们在调用这个方法时,即使参数不是接口中的定义的常量,参数编译器也不会报错。
-
但是对于一个参数以枚举类型定义的方法来说,如果调用这个方法时的参数不是来自于枚举中,那么这个编译器就会报错。
实例3:传统常量与枚举类型常量的区别
定义一个Constants类,和一个ConstantsTest类,在Constants类中定义两个int类型的常量Constants_A和Constants_B,在ConstantsTest类中定义Constants2枚举类,将Constants_A和Constants_B放置其中,最后在ConstantsTest类中声明两个方法doit()和doit2(),分别调用接口中的常量和枚举类型的常量。
public class ConstantsTest {
enum Constants2{
Constants_A,Constants_B;
}
public static void doit(int c) {
switch(c) {
case Constants.Constants_A:
System.out.println("int型常量值1: "+Constants.Constants_A);
break;
case Constants.Constants_B:
System.out.println("int型常量值2: "+Constants.Constants_B);
break;
}
}
public static void doit2(Constants2 c) {
switch(c) {
case Constants_A:
System.out.println("枚举常量值1: "+Constants2.Constants_A);
break;
case Constants_B:
System.out.println("枚举常量值2: "+Constants2.Constants_B);
break;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ConstantsTest.doit(Constants.Constants_A);
ConstantsTest.doit(Constants.Constants_B);
ConstantsTest.doit2(Constants2.Constants_A);
ConstantsTest.doit2(Constants2.Constants_B);
ConstantsTest.doit(3);
//ConstantsTest.doit2(3); //不是枚举中的常量,会在编译时被检查出来
}
}
Console:
int型常量值1: 1
int型常量值2: 66
枚举常量值: Constants_A
枚举常量值: Constants_B