1.1 什么是枚举
Java 1.5引入了一个新特性,枚举!枚举的本质是一个类,用于定义静态常量;
枚举类特点:
·枚举类用特有关键字enum修饰的类,本质是类,很特殊的类
·类不能被创建new对象,不能被继承
·枚举类中写的都是成员变量
·【原理】枚举类中,成员变量被final和static修饰,默认的,不用写
1.2 开发时,如何使用枚举?
/**
* 返回给用户,信息枚举类
*/
public enum MessageEnum {
ACTION_SUCCESS("操作成功"),
ACTION_FAIL("操作失败"),
DELETE_CHECKITEM_FAIL("删除检查项失败"),
DELETE_CHECKITEM_SUCCESS("删除检查项成功"),
ADD_CHECKITEM_SUCCESS("新增检查项成功"),
ADD_CHECKITEM_FAIL("新增检查项失败"),
EDIT_CHECKITEM_FAIL("编辑检查项失败"),
EDIT_CHECKITEM_SUCCESS("编辑检查项成功"),
QUERY_CHECKITEM_SUCCESS("查询检查项成功"),
QUERY_CHECKITEM_FAIL("查询检查项失败"),
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("批量导入预约设置数据失败"),
IMPORT_ORDERSETTING_SUCCESS("批量导入预约设置数据成功"),
GET_ORDERSETTING_SUCCESS("获取预约设置数据成功"),
GET_ORDERSETTING_FAIL("获取预约设置数据失败"),
ORDERSETTING_SUCCESS("预约设置成功"),
ORDERSETTING_FAIL("预约设置失败"),
ADD_MEMBER_FAIL("新增会员失败"),
ADD_MEMBER_SUCCESS("新增会员成功"),
DELETE_MEMBER_FAIL("删除会员失败"),
DELETE_MEMBER_SUCCESS("删除会员成功"),
EDIT_MEMBER_FAIL("编辑会员失败"),
EDIT_MEMBER_SUCCESS("编辑会员成功"),
TELEPHONE_VALIDATECODE_NOTNULL("手机号和验证码都不能为空"),
LOGIN_SUCCESS("登录成功"),
VALIDATECODE_ERROR("验证码输入错误"),
QUERY_ORDER_SUCCESS("查询预约信息成功"),
QUERY_ORDER_FAIL("查询预约信息失败"),
QUERY_SETMEALLIST_SUCCESS("查询套餐列表数据成功"),
QUERY_SETMEALLIST_FAIL("查询套餐列表数据失败"),
QUERY_SETMEAL_SUCCESS("查询套餐数据成功"),
QUERY_SETMEAL_FAIL("查询套餐数据失败"),
SEND_VALIDATECODE_FAIL("验证码发送失败"),
SEND_VALIDATECODE_SUCCESS("验证码发送成功"),
SELECTED_DATE_CANNOT_ORDER("所选日期不能进行体检预约"),
ORDER_FULL("预约已满"),
HAS_ORDERED("不能重复预约"),
ORDER_SUCCESS("预约成功"),
GET_USERNAME_SUCCESS("获取当前登录用户名称成功"),
GET_USERNAME_FAIL("获取当前登录用户名称失败"),
GET_MENU_SUCCESS("获取当前登录用户菜单成功"),
GET_MENU_FAIL("获取当前登录用户菜单失败"),
GET_MEMBER_NUMBER_REPORT_SUCCESS("获取会员统计数据成功"),
GET_MEMBER_NUMBER_REPORT_FAIL("获取会员统计数据失败"),
GET_SETMEAL_COUNT_REPORT_SUCCESS("获取套餐统计数据成功"),
GET_SETMEAL_COUNT_REPORT_FAIL("获取套餐统计数据失败"),
GET_BUSINESS_REPORT_SUCCESS("获取运营统计数据成功"),
GET_BUSINESS_REPORT_FAIL("获取运营统计数据失败"),
GET_SETMEAL_LIST_SUCCESS("查询套餐列表数据成功"),
GET_SETMEAL_LIST_FAIL("查询套餐列表数据失败"),
SETMEAL_GET_TOKEN("套餐获取TOKEN成功"),
ADD_IMGNAME_REDI("添加图片名称到Redis成功");
private final String msg;
MessageEnum(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
}
/**
* 返回信息封装对象Result
*/
public class Result implements Serializable {
private boolean flag;//执行结果,true为执行成功 false为执行失败
private String message;//返回结果信息
private Object data;//返回数据
//Getter、Setter、toString、空参构造、满参构造
}
/**
* 1. 获取客户端手机号及验证码
* 2. 根据手机号获取redis验证码
* 3. 判断用户验证码与redis验证码是否匹配
* 1. 如果匹配失败,返回验证码错误
* 2.如果成功,根据手机号获取会员信息
* 1. 如果有会员,登录成功
* 2. 如果不是会员,封装Member对象,调用Service保存
* @param map
* @return
*/
@RequestMapping("/check")
public Result check(@RequestBody Map<String,String> map){
log.debug("LoginController: check: " + map);
// 获取客户端手机号及验证码
String telephone = map.get("telephone");
String validateCode = map.get("validateCode");
// 读取Redis验证码
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("登录成功, 保存会员信息成功!!!");
return new Result(true, MessageEnum.LOGIN_SUCCESS.getMsg());
}
/**
* 1. 获取客户端手机号及验证码
* 2. 根据手机号获取redis验证码
* 3. 判断用户验证码与redis验证码是否匹配
* 1. 如果匹配失败,返回验证码错误
* 2.如果成功,根据手机号获取会员信息
* 1. 如果有会员,登录成功
* 2. 如果不是会员,封装Member对象,调用Service保存
* @param map
* @return
*/
@RequestMapping("/check")
public Result checkAge(@RequestBody Map<String,String> map){
log.debug("LoginController: check: " + map);
// 获取客户端手机号及验证码
String telephone = map.get("telephone");
String validateCode = map.get("validateCode");
// 读取Redis验证码
String codeInRedis = jedisPool.getResource().get(telephone+ "-"+ RedisConst.SENDTYPE_LOGIN);
if(codeInRedis == null || codeInRedis.length()==0 ){
return new Result(false, "验证码输入不能为空");
}
if ( !codeInRedis.equals(validateCode) ){
return new Result(false, "验证码输入错误");
}
memberService.saveOrEdit(telephone);
log.debug("登录成功, 保存会员信息成功!!!");
return new Result(true, "登录成功");
}
1.3 没有枚举之前,怎么做的?
第一阶段:在类的方法中
·缺陷问题:开发不可能只一个类!
·不能复用
·"大海捞针式"修改局部变量
·无法统一维护系统局部变量
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 = "绿色";
//状态
String save = "新增成功";
String update = "修改成功";
String del = "删除成功";
String find = "查询成功";
}
}
第二阶段:在类的常量,静态static和final修饰
解决问题:
·不能复用
·"大海捞针式"修改局部变量
·缺陷问题:
·不能统一维护系统局部变量
·不遵循类单一职责原则
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 = "绿色";
//状态
public static final String SAVE = "新增成功";
public static final String UPDATE = "修改成功";
public static final String DEL = "删除成功";
public static final String FIND = "查询成功";
}
第三阶段:在静态常量类中
解决问题:
·不能复用
·"大海捞针式"修改局部变量
·不能统一维护系统局部变量
·不遵循类单一职责原则
缺陷问题:
·对自定义类型对象常量支持不是很好,不太好扩展
·类职责,从设计的角度,没有划清界限!有些智障男孩, 总是会用静态常量类去创建对象,或在静态常量类中写业务代码!
/**
* 返回给用户,信息枚举类
*/
public class MessageEnum {
public static final String GET_BUSINESS_REPORT_SUCCESS = "获取运营统计数据成功";
public static final String GET_BUSINESS_REPORT_FAIL = "获取运营统计数据失败";
public static final String GET_SETMEAL_LIST_SUCCESS = "查询套餐列表数据成功";
public static final String GET_SETMEAL_LIST_FAIL = "查询套餐列表数据失败";
public static final String SETMEAL_GET_TOKEN = "套餐获取TOKEN成功";
public static final String ADD_IMGNAME_REDI = "添加图片名称到Redis成功";
//…..
}
1.4 枚举原理解析
枚举的出现解决了之前常量定义的诸多问题:
解决问题:
·不能复用
·"大海捞针式"修改局部变量
·不能统一维护系统局部变量
·不遵循类单一职责原则
·对自定义类型对象常量支持不是很好,不太好扩展
·类职责,从设计的角度,没有划清界限!有些智障男孩, 总是会用静态常量类去创建对象,或在静态常量类中写业务代码!
·枚举类不能创建对象
·枚举类不能继承
public enum WeekEnum {
MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
枚举!枚举的本质是一个类,用于定义静态常量;我们可以通过反编译WeekEnum.class字节码文件,过滤掉编译器"语法糖",就是看到枚举的本来面目,其实就一个类;
反编译工具jad.exe,反编译命令:
jad -sjava WeekEnum.class
WeekEnum枚举类,反编译后的java文件;证明枚举类是个final修饰,内含常量的类
// 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中级程序员培训课程
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/253638.html