1. 枚举常用方法
[Java] 纯文本查看 复制代码 public enum SexEnum {
/**
*
*/
NONE(0, "未知"),
MALE(1, "男"),
FEMALE(2, "女");
private Integer code;
private String message;
SexEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
2. 通过接口来拓展功能
[Java] 纯文本查看 复制代码 public interface IOperation {
double apply(double x, double y);
}
public enum Operation implements IOperation{
PLUS("+") {
@Override
public double apply(double x, double y) {
return x + y;
}
}, MINUS("-") {
@Override
public double apply(double x, double y) {
return x - y;
}
}, TIMES("*") {
@Override
public double apply(double x, double y) {
return x * y;
}
}, DIVIDE("/") {
@Override
public double apply(double x, double y) {
return x / y;
}
};
private final String symbol;
Operation(String symbol) {
this.symbol = symbol;
}
|