我们可以通过在枚举类型中定义属性,方法和构造函数让它变得更加强大。
当我们调用短信验证码的时候可能有几种不同的用途,我们在下面这样定义:
package com.test.enums;
public enum SmsType {
REGISTER(100000, "注册使用"),
FORGET_PASSWORD(100001, "忘记密码使用"),
UPDATE_PHONE_NUMBER(100002, "更新手机号码使用");
private final int code;
private final String message;
SmsType(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return "PinType{" +
"code=" + code +
", message='" + message + '\'' +
'}';
}
public static void main(String[] args) {
System.out.println(SmsType.FORGET_PASSWORD.getCode()); //100001
System.out.println(SmsType.FORGET_PASSWORD.getMessage()); //忘记密码使用
System.out.println(SmsType.FORGET_PASSWORD.toString()); //PinType{code=100001, message='忘记密码使用'}
}
}