前言:
https://www.cnblogs.com/LoveBB/p/17277662.html
什么是列舉
枚:量詞,一般用于較小的片狀物,相當于“個”,
舉:提出:列舉,舉一反三,舉個例子,
所以,列舉就是一個個列舉出來
列舉的作用
魔功的作用,就不過多描述了,主打的就是一個優雅,
列舉的使用
列舉的成員方法
在學習列舉之前,我們很有必要了解他的幾個比較重要的成員方法,我們先定義一個列舉如下:
public enum TestEnum {
}
- 這里可以看到,列舉中有兩個比較重要的方法,分別是 values() 和 valueOf();

| 方法名稱 | 具體含義 | 使用方法 |
|---|---|---|
| values() | 該方法可以將列舉型別成員以陣列的形式回傳 | 列舉型別名稱.values() |
| valueOf() | 該方法可以實作將普通字串轉換為列舉實體 | 列舉型別名稱.valueOf("ABC") |
- 下面我們隨便在列舉中添加兩個屬性 boy 和 girl
// 規范寫法
public enum TestEnum {
BOY,GIRL
}
// 屬性大寫只是一種規范,小寫也是可以的
public enum TestEnum {
Boy,Girl
}
// 既然小寫可以,那么亂寫也是沒有問題的
public enum TestEnum {
BoY,giRL
}
- 我們使用規范的方法,試試列舉中的 values() 方法,這個方法是拿到 TestEnum 中的所有屬性,并且以陣列的形式回傳,

- 試試列舉中的 values() 方法

從上面可以看到,這三者的回傳值是一樣的,型別都是 TestEnum,那這個方法就沒啥好看的了,就是拿到 TestEnum 中的單個屬性,
列舉與常量類的對比
列舉類
public enum TestEnum {
BOY,GIRL
}
常量類
public class TestConstant {
private static Integer BOY;
private static Integer GIRL;
}
- 列舉和常量類的對比一目了然,但是一般情況下,我們使用常量類都是下面這種形式,
public class TestConstant {
// 男
private static final Integer BOY = 0;
// 女
private static Integer GIRL = 1;
}
- 這樣看起來是不是更加親切了,那么列舉類應該怎么寫呢,效果如下
@Getter
public enum TestEnum {
BOY(0),GIRL(1);
private Integer code;
TestEnum(Integer code){
this.code = code;
}
}
- 兩者使用對比
public static void main(String[] args) {
// 使用常量類
Integer boy = TestConstant.BOY;
Integer girl = TestConstant.GIRL;
// 使用列舉
Integer enumBoy = TestEnum.BOY.getCode();
Integer girlCode = TestEnum.GIRL.getCode();
}
列舉與常量類的使用
- 眾所周知,我們對前端展示的 性別 這個屬性,一般在資料表中都是存 0,1,這樣的話,每次查詢和入庫都需要做轉換,
@Data
public class People {
private String gender;
}
- 常量類轉化
People people = new People();
// int 轉為 String
Integer boy = TestConstant.BOY;
if (boy == 0) {
people.setGender("男");
}
Integer girl = TestConstant.GIRL;
if (girl == 1) {
people.setGender("女");
}
- 列舉轉化
People people = new People();
// 使用列舉
Integer enumBoy = TestEnum.BOY.getCode();
if (enumBoy == 0) {
people.setGender("男");
}
Integer girlCode = TestEnum.GIRL.getCode();
if (girlCode == 1) {
people.setGender("女");
}
毫無違和感,那還要列舉干啥勒,別著急,下面就介紹列舉的另一種用法 :
@Getter
public enum TestEnum {
BOY(0,"男"),GIRL(1,"女");
private Integer code;
private String name;
TestEnum(Integer code,String name){
this.code = code;
}
}
- 列舉轉化 2
People people = new People();
// 使用列舉
Integer enumBoy = TestEnum.BOY.getCode();
if (enumBoy == 0) {
String name = TestEnum.BOY.getName();
people.setGender(name);
}
Integer girlCode = TestEnum.GIRL.getCode();
if (girlCode == 1) {
String name = TestEnum.GIRL.getName();
people.setGender(name);
}
真好,如果工資是按代碼行數發的,那你就能多拿兩打工資了
- 列舉轉化3
上面 2 的寫法,基本上都是固定了,不符合面向物件的寫法,我們應該抽象出來這一個方法,方法的入參是 int 型別,回傳值是 String 型別,比如傳入一個 0,回傳一個男,傳入一個 1,回傳一個女,這個時候前面的這個 values() 方法就派上用場了
@Getter
public enum TestEnum {
BOY(0,"男"),GIRL(1,"女");
private Integer code;
private String name;
TestEnum(Integer code,String name){
this.code = code;
}
// 這個是抽象的方法,放在列舉里面
public static String getName(int code) {
// 遍歷 TestEnum.values()
for (TestEnum testEnum : TestEnum.values()) {
// 如果列舉 code 和傳入的 code相同,回傳對應的文字
if (testEnum.getCode() == code) {
return testEnum.name;
}
}
// 不匹配,回傳默認值
return null;
}
}
- 使用方法改造
People people = new People();
// 使用列舉
Integer enumBoy = TestEnum.BOY.getCode();
if (enumBoy == 0) {
people.setGender(TestEnum.getName(0));
}
Integer girlCode = TestEnum.GIRL.getCode();
if (girlCode == 1) {
people.setGender(TestEnum.getName(1));
}
是不是有高手那味了,這就是魔功列舉的強大之處,隨隨便便一個常量,就硬生生給你整成高手范,
列舉的進階
- 完了嘛?如果只是簡單的使用,確實是完了,但是我們的追求遠不止如此,我們用了列舉,就一定要用出高手風范:一切皆可列舉
三屬性列舉
要獲取每個網站的地址,要把每個網站的地址存盤起來,網站地址都是已知的,并且不做維護,
- 如果要建立一張表,大概如下:

- 如果我們用列舉來實作,就是下面這樣
@Getter
public enum InternetEnum {
BAIDU(1,"百度","www.baidu.com"),
HUOHU(2,"火狐","www.huohu.com"),
GUGLE(3,"谷歌","www.guge.com"),
SLO(4,"360","www.360.com"),
QQ(5,"qq","www.qq.com");
private Integer code;
private String name;
private String address;
private InternetEnum(int code,String name,String address) {
this.code = code;
this.name = name;
this.address = address;
}
private String getName(int code){
for (InternetEnum internetEnum : InternetEnum.values()) {
if (internetEnum.getCode()==code){
return internetEnum.getName();
}
}
return null;
}
private String getAddress(int code){
for (InternetEnum internetEnum : InternetEnum.values()) {
if (internetEnum.getCode()==code){
return internetEnum.getAddress();
}
}
return null;
}
}
這里只是舉個例子,前提是這些屬性不經常變的,如果是經常變的,還是得考慮使用資料表,至于哪些是不變的,如果你維護過別人的代碼,就一定能見過很多這樣的東西,這時候大膽使用列舉改掉他,
N屬性
- 有了三屬性,就有四屬性,五屬性,不知道,當你看到下面這種列舉的時候,你會不會陷入沉思,并且優雅親切來上一句問候

列舉神奇之處
- 列舉用順手之后,萬物皆可列舉
- 對前端:傳 0,1 入參就行
- 對資料表,咱就存 0,1 就行
- 這樣一來,主動權就在你手里了,當別人看著資料表一堆 0,1 陷入沉思的時候、當前端其他人來接手你的代碼時,前端傳給他一堆 0,1的時候,就是魔功顯示威力的時候,

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/549125.html
標籤:其他
上一篇:存盤引擎和資料型別
