java枚举的高级用法
1. 枚举常用方法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. 通过接口来拓展功能
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;
}
搞了这么久JAVA , 刚知道enum 也可以实现接口.. 学习了。 枚举就是个类,除了继承(可以认为他已经有了基类且自身为final的),所有类能实现的功能它都能实现
页:
[1]