# 优雅编程之这样使用枚举和注解，你就“正常”了（二十九）

2017-01-13 14:58:48来源:csdn作者:huangwenyi1010人点击

* 状态枚举
* @author Ay
*/
public enum StatusEnum {
/** 0：已删除 */
DELETED("0","已删除"),
/** 1：启用 */
ENABLE("1","启用"),
/** 2：禁用 */
DISABLE("2","禁用");/** 状态值 */
private final String code;private final String name;private StatusEnum(String code, String name) {
this.code = code;
this.name = name;
}@Override
public String toString() {
return code;
}public String getName() {
return name;
}
}

* Created by Ay on 2016/10/2.
*/
public enum Operation {PLUS,MINUS,TIMES,DIVIDE;double apply(double x,double y){
switch (this){
case PLUS:return x + y;
case MINUS:return x - y;
case TIMES:return x * y;
case DIVIDE:return x / y;
}
throw new AssertionError("Unknow op: " + this);
}
}

* Created by Ay on 2016/10/2.
*/
public enum Operation {PLUS("+"){
@Override
double apply(double x, double y) {
return x + y;
}
},
MINUS("-"){
@Override
double apply(double x, double y) {
return x - y;
}
},
TIMES("*"){
@Override
double apply(double x, double y) {
return x * y;
}
},
DIVIDE("/"){
@Override
double apply(double x, double y) {
return x / y;
}
};private final String symbol;
Operation(String symbol){this.symbol = symbol;}@Override
public String toString() { return this.symbol;}
abstract double apply(double x,double y);
}

/**
* Created by Ay on 2016/10/2.
*/
public enum PayrollDay {MonDAY(PayType.WEEKDAY),
WEEKDAY(PayType.WEEKDAY),
TUESDAY(PayType.WEEKDAY),
WENDESDAY(PayType.WEEKDAY),
THURSDAY(PayType.WEEKDAY),
FRIDAY(PayType.WEEKDAY),
SATURDAY(PayType.WEEKEND),
SUNDAY(PayType.WEEKEND);private PayType payType;PayrollDay(PayType payType){ this.payType = payType;}double pay(double hoursWorked,double payRate){
return payType.pay(hoursWorked,payRate);
}
//这里是嵌套枚举
private enum PayType{WEEKDAY{
@Override
double overtimePay(double hrs, double payRate) {
return 0;
}
},WEEKEND{
@Override
double overtimePay(double hrs, double payRate) {
return 0;
}
};private static final int HOURS_PRE_SHIFT = 8;abstract double overtimePay(double hrs,double payRate);double pay(double hoursWorked,double payRate){
double basePay = hoursWorked * payRate;
return basePay + overtimePay(hoursWorked,payRate);
}
}
}

enum Ensemble{
SOLO,DUET,TRIO,QUARTET,QUINTET,SEXTET,SEPTEX,OCTEX,NONET,DECTET;
public int numberOfMusicians(){
return ordinal() + 1;//ordinal()用来返回枚举常量在类型中的数字位置。
}
}

enum Ensemble{
SOLO(1),DUET(2),TRIO(3),QUARTET(4),QUINTET(5),
SEXTET(6),SEPTEX(7),OCTEX(8),NONET(9),DECTET(10);private final int numberOfMusicians;Ensemble(int size){
this.numberOfMusicians = size;
}public int numberOfMusicians(){
return ordinal() + 1;
}}

Enum规范中写道：大多数程序员都不需要这个方法。除非你在编写的是这种数据结构，否则最好避免使用original方法。

class Text{
public static final int STYLE_BOLE = 1 << 0;
public static final int STYLE_ITALIC = 1 <<1;
public static final int STYLE_UNDERLINE = 1 <<2;
public static final int STYLE_STRIKETHROUGH = 1 <<3;
public void applyStyles(int styles){ ... }
}

class Text{public enum Style{BOLE,ITALIC,UNDERLINE,STRIKETHROUGH}
public void applyStyles(Set<Style> styles){}
}

public interface Operation{
double apply(double x,double y);
}public enum BasicOperation implements Operation{PLUS("+"){
public double apply(double x,double y){ return x + y;}
},
MINUS("-"){
public double apply(double x,double y){ return x - y;}
},
TIMES("*"){
public double apply(double x,double y){ return x * y;}
},
DIVIDE("/"){
public double apply(double x,double y){ return x / y;}
};private final String symbool;
BasicOperation(String symbool){ this.symbool = symbool}@Override
public String toString() {
return symbool;
}@Override
public double apply(double x, double y) {
return 0;
}
}

public interface Operation{
double apply(double x,double y);
}public enum BasicOperation implements Operation{PLUS("^"){
public double apply(double x,double y){ return Math.pow(x,y);}
},
MINUS("%"){
public double apply(double x,double y){ return x % y;}
};private final String symbool;
BasicOperation(String symbool){ this.symbool = symbool}@Override
public String toString() {
return symbool;
}@Override
public double apply(double x, double y) {
return 0;
}
}

@Test
public void testLove(){
System.out.println("I love you!!!");
}

@Override只能用在方法声明中，它表示被注解的方法声明覆盖了超类型中的一个声明。如果坚持使用这个注解，可以防止一大类的非法错误。