更新時(shí)間:2020-07-08 來源:黑馬程序員 瀏覽量:
1.1 什么是枚舉
Java 1.5引入了一個(gè)新特性,枚舉!枚舉的本質(zhì)是一個(gè)類,用于定義靜態(tài)常量;
枚舉類特點(diǎn):
·枚舉類用特有關(guān)鍵字enum修飾的類,本質(zhì)是類,很特殊的類
·類不能被創(chuàng)建new對(duì)象,不能被繼承
·枚舉類中寫的都是成員變量
·【原理】枚舉類中,成員變量被final和static修飾,默認(rèn)的,不用寫
1.2 開發(fā)時(shí),如何使用枚舉?
/**
* 返回給用戶,信息枚舉類
*/
public enum MessageEnum {
ACTION_SUCCESS("操作成功"),
ACTION_FAIL("操作失敗"),
DELETE_CHECKITEM_FAIL("刪除檢查項(xiàng)失敗"),
DELETE_CHECKITEM_SUCCESS("刪除檢查項(xiàng)成功"),
ADD_CHECKITEM_SUCCESS("新增檢查項(xiàng)成功"),
ADD_CHECKITEM_FAIL("新增檢查項(xiàng)失敗"),
EDIT_CHECKITEM_FAIL("編輯檢查項(xiàng)失敗"),
EDIT_CHECKITEM_SUCCESS("編輯檢查項(xiàng)成功"),
QUERY_CHECKITEM_SUCCESS("查詢檢查項(xiàng)成功"),
QUERY_CHECKITEM_FAIL("查詢檢查項(xiàng)失敗"),
UPLOAD_SUCCESS("上傳成功"),
ADD_CHECKGROUP_FAIL("新增檢查組失敗"),
ADD_CHECKGROUP_SUCCESS("新增檢查組成功"),
DELETE_CHECKGROUP_FAIL("刪除檢查組失敗"),
DELETE_CHECKGROUP_SUCCESS("刪除檢查組成功"),
QUERY_CHECKGROUP_SUCCESS("查詢檢查組成功"),
QUERY_CHECKGROUP_FAIL("查詢檢查組失敗"),
EDIT_CHECKGROUP_FAIL("編輯檢查組失敗"),
EDIT_CHECKGROUP_SUCCESS("編輯檢查組成功"),
PIC_UPLOAD_SUCCESS("圖片上傳成功"),
PIC_UPLOAD_FAIL("圖片上傳失敗"),
ADD_SETMEAL_FAIL("新增套餐失敗"),
ADD_SETMEAL_SUCCESS("新增套餐成功"),
IMPORT_ORDERSETTING_FAIL("批量導(dǎo)入預(yù)約設(shè)置數(shù)據(jù)失敗"),
IMPORT_ORDERSETTING_SUCCESS("批量導(dǎo)入預(yù)約設(shè)置數(shù)據(jù)成功"),
GET_ORDERSETTING_SUCCESS("獲取預(yù)約設(shè)置數(shù)據(jù)成功"),
GET_ORDERSETTING_FAIL("獲取預(yù)約設(shè)置數(shù)據(jù)失敗"),
ORDERSETTING_SUCCESS("預(yù)約設(shè)置成功"),
ORDERSETTING_FAIL("預(yù)約設(shè)置失敗"),
ADD_MEMBER_FAIL("新增會(huì)員失敗"),
ADD_MEMBER_SUCCESS("新增會(huì)員成功"),
DELETE_MEMBER_FAIL("刪除會(huì)員失敗"),
DELETE_MEMBER_SUCCESS("刪除會(huì)員成功"),
EDIT_MEMBER_FAIL("編輯會(huì)員失敗"),
EDIT_MEMBER_SUCCESS("編輯會(huì)員成功"),
TELEPHONE_VALIDATECODE_NOTNULL("手機(jī)號(hào)和驗(yàn)證碼都不能為空"),
LOGIN_SUCCESS("登錄成功"),
VALIDATECODE_ERROR("驗(yàn)證碼輸入錯(cuò)誤"),
QUERY_ORDER_SUCCESS("查詢預(yù)約信息成功"),
QUERY_ORDER_FAIL("查詢預(yù)約信息失敗"),
QUERY_SETMEALLIST_SUCCESS("查詢套餐列表數(shù)據(jù)成功"),
QUERY_SETMEALLIST_FAIL("查詢套餐列表數(shù)據(jù)失敗"),
QUERY_SETMEAL_SUCCESS("查詢套餐數(shù)據(jù)成功"),
QUERY_SETMEAL_FAIL("查詢套餐數(shù)據(jù)失敗"),
SEND_VALIDATECODE_FAIL("驗(yàn)證碼發(fā)送失敗"),
SEND_VALIDATECODE_SUCCESS("驗(yàn)證碼發(fā)送成功"),
SELECTED_DATE_CANNOT_ORDER("所選日期不能進(jìn)行體檢預(yù)約"),
ORDER_FULL("預(yù)約已滿"),
HAS_ORDERED("不能重復(fù)預(yù)約"),
ORDER_SUCCESS("預(yù)約成功"),
GET_USERNAME_SUCCESS("獲取當(dāng)前登錄用戶名稱成功"),
GET_USERNAME_FAIL("獲取當(dāng)前登錄用戶名稱失敗"),
GET_MENU_SUCCESS("獲取當(dāng)前登錄用戶菜單成功"),
GET_MENU_FAIL("獲取當(dāng)前登錄用戶菜單失敗"),
GET_MEMBER_NUMBER_REPORT_SUCCESS("獲取會(huì)員統(tǒng)計(jì)數(shù)據(jù)成功"),
GET_MEMBER_NUMBER_REPORT_FAIL("獲取會(huì)員統(tǒng)計(jì)數(shù)據(jù)失敗"),
GET_SETMEAL_COUNT_REPORT_SUCCESS("獲取套餐統(tǒng)計(jì)數(shù)據(jù)成功"),
GET_SETMEAL_COUNT_REPORT_FAIL("獲取套餐統(tǒng)計(jì)數(shù)據(jù)失敗"),
GET_BUSINESS_REPORT_SUCCESS("獲取運(yùn)營統(tǒng)計(jì)數(shù)據(jù)成功"),
GET_BUSINESS_REPORT_FAIL("獲取運(yùn)營統(tǒng)計(jì)數(shù)據(jù)失敗"),
GET_SETMEAL_LIST_SUCCESS("查詢套餐列表數(shù)據(jù)成功"),
GET_SETMEAL_LIST_FAIL("查詢套餐列表數(shù)據(jù)失敗"),
SETMEAL_GET_TOKEN("套餐獲取TOKEN成功"),
ADD_IMGNAME_REDI("添加圖片名稱到Redis成功");
private final String msg;
MessageEnum(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
}
/**
* 返回信息封裝對(duì)象Result
*/
public class Result implements Serializable {
private boolean flag;//執(zhí)行結(jié)果,true為執(zhí)行成功 false為執(zhí)行失敗
private String message;//返回結(jié)果信息
private Object data;//返回?cái)?shù)據(jù)
//Getter、Setter、toString、空參構(gòu)造、滿參構(gòu)造
}
/**
* 1. 獲取客戶端手機(jī)號(hào)及驗(yàn)證碼
* 2. 根據(jù)手機(jī)號(hào)獲取redis驗(yàn)證碼
* 3. 判斷用戶驗(yàn)證碼與redis驗(yàn)證碼是否匹配
* 1. 如果匹配失敗,返回驗(yàn)證碼錯(cuò)誤
* 2.如果成功,根據(jù)手機(jī)號(hào)獲取會(huì)員信息
* 1. 如果有會(huì)員,登錄成功
* 2. 如果不是會(huì)員,封裝Member對(duì)象,調(diào)用Service保存
* @param map
* @return
*/
@RequestMapping("/check")
public Result check(@RequestBody Map<String,String> map){
log.debug("LoginController: check: " + map);
// 獲取客戶端手機(jī)號(hào)及驗(yàn)證碼
String telephone = map.get("telephone");
String validateCode = map.get("validateCode");
// 讀取Redis驗(yàn)證碼
String codeInRedis = jedisPool.getResource().get(telephone+ "-"+ RedisConst.SENDTYPE_LOGIN);
if(codeInRedis == null || codeInRedis.length()==0 ){
return new Result(false, MessageEnum.TELEPHONE_VALIDATECODE_NOTNULL.getMsg());
}
if ( !codeInRedis.equals(validateCode) ){
return new Result(false, MessageEnum.VALIDATECODE_ERROR.getMsg());
}
memberService.saveOrEdit(telephone);
log.debug("登錄成功, 保存會(huì)員信息成功!!!");
return new Result(true, MessageEnum.LOGIN_SUCCESS.getMsg());
}
/**
* 1. 獲取客戶端手機(jī)號(hào)及驗(yàn)證碼
* 2. 根據(jù)手機(jī)號(hào)獲取redis驗(yàn)證碼
* 3. 判斷用戶驗(yàn)證碼與redis驗(yàn)證碼是否匹配
* 1. 如果匹配失敗,返回驗(yàn)證碼錯(cuò)誤
* 2.如果成功,根據(jù)手機(jī)號(hào)獲取會(huì)員信息
* 1. 如果有會(huì)員,登錄成功
* 2. 如果不是會(huì)員,封裝Member對(duì)象,調(diào)用Service保存
* @param map
* @return
*/
@RequestMapping("/check")
public Result checkAge(@RequestBody Map<String,String> map){
log.debug("LoginController: check: " + map);
// 獲取客戶端手機(jī)號(hào)及驗(yàn)證碼
String telephone = map.get("telephone");
String validateCode = map.get("validateCode");
// 讀取Redis驗(yàn)證碼
String codeInRedis = jedisPool.getResource().get(telephone+ "-"+ RedisConst.SENDTYPE_LOGIN);
if(codeInRedis == null || codeInRedis.length()==0 ){
return new Result(false, "驗(yàn)證碼輸入不能為空");
}
if ( !codeInRedis.equals(validateCode) ){
return new Result(false, "驗(yàn)證碼輸入錯(cuò)誤");
}
memberService.saveOrEdit(telephone);
log.debug("登錄成功, 保存會(huì)員信息成功!!!");
return new Result(true, "登錄成功");
}
1.3 沒有枚舉之前,怎么做的?
·第一階段:在類的方法中
·缺陷問題:開發(fā)不可能只一個(gè)類!
·不能復(fù)用
·"大海撈針式"修改局部變量
·無法統(tǒng)一維護(hù)系統(tǒng)局部變量
public class WeekValue {
public static void main(String[] args) {
//第一階段:定義局部變量,字面值
String one = "星期1";
String one1 = "星期一";
String two = "星期二";
String three = "星期三";
String four = "星期四";
String five = "星期五";
String six = "星期六";
String seven = "星期七";
//性別
String man = "男";
String women = "女";
//顏色
String red = "紅色";
String yellow = "黃色";
String green = "綠色";
//狀態(tài)
String save = "新增成功";
String update = "修改成功";
String del = "刪除成功";
String find = "查詢成功";
}
}
第二階段:在類的常量,靜態(tài)static和final修飾
解決問題:
·不能復(fù)用
·"大海撈針式"修改局部變量
·缺陷問題:
·不能統(tǒng)一維護(hù)系統(tǒng)局部變量
·不遵循類單一職責(zé)原則
public class WeekValue {
public static final String ONE = "星期一";
public static final String ONE1 = "星期一";
public static final String TWO = "星期二";
public static final String THREE = "星期三";
public static final String FOUR = "星期四";
public static final String FIVE = "星期五";
public static final String SIX = "星期六";
public static final String SEVEN = "星期七";
//性別
public static final String MAN = "男";
public static final String WOMEN = "女";
//顏色
public static final String RED = "紅色";
public static final String YELLOW = "黃色";
public static final String GREEN = "綠色";
//狀態(tài)
public static final String SAVE = "新增成功";
public static final String UPDATE = "修改成功";
public static final String DEL = "刪除成功";
public static final String FIND = "查詢成功";
}
第三階段:在靜態(tài)常量類中
解決問題:
·不能復(fù)用
·"大海撈針式"修改局部變量
·不能統(tǒng)一維護(hù)系統(tǒng)局部變量
·不遵循類單一職責(zé)原則
缺陷問題:
·對(duì)自定義類型對(duì)象常量支持不是很好,不太好擴(kuò)展
·類職責(zé),從設(shè)計(jì)的角度,沒有劃清界限!有些智障男孩, 總是會(huì)用靜態(tài)常量類去創(chuàng)建對(duì)象,或在靜態(tài)常量類中寫業(yè)務(wù)代碼!
/**
* 返回給用戶,信息枚舉類
*/
public class MessageEnum {
public static final String GET_BUSINESS_REPORT_SUCCESS = "獲取運(yùn)營統(tǒng)計(jì)數(shù)據(jù)成功";
public static final String GET_BUSINESS_REPORT_FAIL = "獲取運(yùn)營統(tǒng)計(jì)數(shù)據(jù)失敗";
public static final String GET_SETMEAL_LIST_SUCCESS = "查詢套餐列表數(shù)據(jù)成功";
public static final String GET_SETMEAL_LIST_FAIL = "查詢套餐列表數(shù)據(jù)失敗";
public static final String SETMEAL_GET_TOKEN = "套餐獲取TOKEN成功";
public static final String ADD_IMGNAME_REDI = "添加圖片名稱到Redis成功";
//.....
}
1.4 枚舉原理解析
枚舉的出現(xiàn)解決了之前常量定義的諸多問題:
解決問題:
·不能復(fù)用
·"大海撈針式"修改局部變量
·不能統(tǒng)一維護(hù)系統(tǒng)局部變量
·不遵循類單一職責(zé)原則
·對(duì)自定義類型對(duì)象常量支持不是很好,不太好擴(kuò)展
·類職責(zé),從設(shè)計(jì)的角度,沒有劃清界限!有些智障男孩, 總是會(huì)用靜態(tài)常量類去創(chuàng)建對(duì)象,或在靜態(tài)常量類中寫業(yè)務(wù)代碼!
·枚舉類不能創(chuàng)建對(duì)象
·枚舉類不能繼承
public enum WeekEnum {
MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
枚舉!枚舉的本質(zhì)是一個(gè)類,用于定義靜態(tài)常量;我們可以通過反編譯WeekEnum.class字節(jié)碼文件,過濾掉編譯器"語法糖",就是看到枚舉的本來面目,其實(shí)就一個(gè)類;
反編譯工具jad.exe,反編譯命令:
jad -sjava WeekEnum.class
WeekEnum枚舉類,反編譯后的java文件;證明枚舉類是個(gè)final修飾,內(nèi)含常量的類
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: WeekEnum.java
public final class WeekEnum extends Enum
{
public static WeekEnum[] values()
{
return (WeekEnum[])$VALUES.clone();
}
public static WeekEnum valueOf(String name)
{
return (WeekEnum)Enum.valueOf(WeekEnum, name);
}
private WeekEnum(String s, int i)
{
super(s, i);
}
public static final WeekEnum MONDAY;
public static final WeekEnum TUESDAY;
public static final WeekEnum WEDNESDAY;
public static final WeekEnum THURSDAY;
public static final WeekEnum FRIDAY;
public static final WeekEnum SATURDAY;
public static final WeekEnum SUNDAY;
private static final WeekEnum $VALUES[];
static
{
MONDAY = new WeekEnum("MONDAY", 0);
TUESDAY = new WeekEnum("TUESDAY", 1);
WEDNESDAY = new WeekEnum("WEDNESDAY", 2);
THURSDAY = new WeekEnum("THURSDAY", 3);
FRIDAY = new WeekEnum("FRIDAY", 4);
SATURDAY = new WeekEnum("SATURDAY", 5);
SUNDAY = new WeekEnum("SUNDAY", 6);
$VALUES = (new WeekEnum[] {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
});
}
}
猜你喜歡:
Java中級(jí)程序員培訓(xùn)課程