在Java引入枚举类型之前,我们常常通过抽象类的方式定义全局常量,例如:
public abstract class UploadStatus {
public static final int UPLOAD_IN_PROGRESS = 0;
public static final int UPLOAD_FAILED = 1;
public static final int UPLOAD_SUCCEEDED = 2;
}
现在我们可以使用枚举类型来定义:
enum UploadStatusEnum {
UPLOAD_IN_PROGRESS, UPLOAD_FAILED, UPLOAD_SUCCEEDED
}
枚举本身也是一个类,并且统一继承自java.lang.Enum。在枚举类中定义的UPLOAD_IN_PROGRESS, UPLOAD_FAILED, UPLOAD_SUCCEEDED可以理解为UploadStatusEnum的实例(全局唯一的单例)(在定义枚举属性时,要求必须在最前端定义)。接下来,我们看看枚举类型都有哪些属性和方法:
public class TestEnum {
public static void main(String[] args) {
System.out.println(UploadStatusEnum.UPLOAD_IN_PROGRESS);
System.out.println(UploadStatusEnum.UPLOAD_FAILED);
System.out.println(UploadStatusEnum.UPLOAD_SUCCEEDED);
for (UploadStatusEnum uploadStatus : UploadStatusEnum.values()) {
System.out.println("name is " + uploadStatus.name() + " ordinal is " + uploadStatus.ordinal() + " toString is " + uploadStatus.toString());
}
UploadStatusEnum copyEnum = UploadStatusEnum.UPLOAD_IN_PROGRESS;
System.out.println(copyEnum == UploadStatusEnum.UPLOAD_IN_PROGRESS);
System.out.println(copyEnum.equals(UploadStatusEnum.UPLOAD_IN_PROGRESS));
}
enum UploadStatusEnum {
UPLOAD_IN_PROGRESS, UPLOAD_FAILED, UPLOAD_SUCCEEDED
}
}
输出如下:
通过上面的代码可以知道如下几点:
(1)我们可以直接通过UploadStatusEnum.UPLOAD_IN_PROGRESS的方式获取到具体的枚举对象;
(2)调用该对象的toString方法返回的就是枚举属性字面的字符串:UPLOAD_IN_PROGRESS;
(3)我们可以通过UploadStatusEnum.values()静态方法去遍历枚举对象;
(4)通过枚举对象的name()方法同样可以获取到枚举属性字面的字符串(结果和toString方法一致);
(5)通过枚举对象的ordinal()方法可以获取到给枚举对象分配的整型值,按照声明的顺序,从0开始;
(6)枚举对象的比较可以直接通过==或者是equals()方法完成;
知道以上几点,基本上就能够满足我们在开发中使用枚举的大部分情况了。
下面介绍一下枚举的其他用法:
1. 枚举类中自定义构造函数及其他方法:
enum UploadStatusEnum {
UPLOAD_IN_PROGRESS, UPLOAD_FAILED, UPLOAD_SUCCEEDED; // 枚举类如果要自定义构造函数和方法等,必须添加分号,同时枚举属性的声明依旧必须在最顶端
private int value;
private String description;
private UploadStatusEnum(int value, String description) {
this.value = value;
this.description = description;
}
}
上面代码为枚举类自定义了一个私有的构造函数(枚举类的构造函数必须声明为私有的)。但是此时我们的IDE会报错:
Expected 2 arguments but found 0
说明我们自定义的构造函数导致默认的构造函数失效了,因此我们必须修改枚举属性的定义方式:
enum UploadStatusEnum {
UPLOAD_IN_PROGRESS(0, "uploading"), UPLOAD_FAILED(1, "upload failed"), UPLOAD_SUCCEEDED(2, "upload successfully");
private int value;
private String description;
private UploadStatusEnum(int value, String description) {
this.value = value;
this.description = description;
}
}
枚举类中还可以自定义方法和重写父类Enum中的方法,例如:
enum UploadStatusEnum {
UPLOAD_IN_PROGRESS(0, "uploading"), UPLOAD_FAILED(1, "upload failed"), UPLOAD_SUCCEEDED(2, "upload successfully");
private int value;
private String description;
private UploadStatusEnum(int value, String description) {
this.value = value;
this.description = description;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "UploadStatusEnum{" +
"value=" + value +
", description='" + description + '\'' +
'}';
}
}
通过枚举实例调用对应的方法:
System.out.println(UploadStatusEnum.UPLOAD_IN_PROGRESS.getDescription()); // uploading
通过以上的代码可以看出,枚举类的行为和普通类基本上是完全一致的。枚举属性也可以理解为单例对象。
2. 实现接口
由于枚举类已经默认继承自java.lang.Enum类,所以枚举类无法再继承其他的类。但是枚举类还可以实现接口,并实现对应的方法。
3. 枚举集合
枚举集合总共有两个类:java.util.EnumSet(以枚举对象为元素,并且不会重复)和java.util.EnumMap(类似于HashMap,key是枚举对象,值可以是声明的任意类型)。
EnumSet是一个抽象类,无法被实例化:
public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
implements Cloneable, java.io.Serializable
但是我们可以调用它的静态方法:
EnumSet<UploadStatusEnum> statusEnums = EnumSet.allOf(UploadStatusEnum.class);
for (UploadStatusEnum status : statusEnums) {
System.out.println(status);
}
输出如下:
EnumMap是一个继承自AbstractMap<K, V>的类:
public class EnumMap<K extends Enum<K>, V> extends AbstractMap<K, V>
implements java.io.Serializable, Cloneable
使用方法和HashMap类似,例如:
EnumMap<UploadStatusEnum, String> enumMaps = new EnumMap<UploadStatusEnum, String>(UploadStatusEnum.class);
enumMaps.put(UploadStatusEnum.UPLOAD_IN_PROGRESS, "uploading_0");
enumMaps.put(UploadStatusEnum.UPLOAD_FAILED, "uploadFailed_1");
enumMaps.put(UploadStatusEnum.UPLOAD_SUCCEEDED, "uploadSuccess_2");
for (Map.Entry<UploadStatusEnum, String> entry : enumMaps.entrySet()) {
System.out.println("key is " + entry.getKey() + " value is " + entry.getValue());
}
输出结果如下:
以上就是java枚举的全部内容啦~