- 枚举
- 声明
- 枚举遍历
- 枚举在switch中使用
- 枚举比较
- 枚举静态构造方法
- 使用类来模拟枚举
- 枚举中定义抽象方法
- 枚举实现接口
- 单例模式
- 使用静态代码快
- EnumSet
- EnumMap
- 结语
枚举
声明
基本使用
public enum ChineseNumber {
YI,
ER,
SAN,
SI
}
ChineseNumber.values(); // ["YI","ER","SAN","SI"]
枚举遍历
for (ChineseNumber s : ChineseNumber.values()) {
System.out.println(s.name());
}
枚举在switch中使用
ChineseNumber s = ChineseNumber.ER;
switch(s) {
case YI:
System.out.println("一");
break;
case ER:
System.out.println("二");
break;
case SAN:
System.out.println("三");
break;
case SI:
System.out.println("四");
break;
}
枚举比较
Boolean b = ChineseNumber.YI == ChineseNumber.ER;
Boolean b = ChineseNumber.YI.equals(ChineseNumber.ER);
枚举静态构造方法
public enum ChineseNumber {
YI(1),
ER(2),
SAN(3),
SI(4);
private int value;
ChineseNumber(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
ChineseNumber.YI.getValue(); // 1
使用类来模拟枚举
class MyEnum<T extends MyEnum<T>> implements Comparable<T>, Serializable{
public static final MyEnum ONE = new MyEnum(1);
public static final MyEnum TWO = new MyEnum(2);
public static final MyEnum THERE = new MyEnum(3);
public static final MyEnum FOUR = new MyEnum(4);
private int value;
private MyEnum(int value){
this.value = value;
}
public int getValue() {
return value;
}
}
MyEnum.TWO.getValue();
枚举中定义抽象方法
public enum MyAction {
fn {
public String execute() {
return "execute this method";
}
};
public abstract String execute();
};
MyAction.fn.execute();
枚举实现接口
public enum RegEx implements Predicate<String> {
LETTERS("[a-zA-Z]+");
private final Pattern pattern;
private RegEx(final String pattern) {
this.pattern = Pattern.compile(pattern);
}
@Override
public boolean test(final String input) {
return this.pattern.matcher(input).matches();
}
}
RegEx.LETTERS.test("abc");
或者
public enum RegEx implements Predicate<String> {
ISLETTERS {
@Override
public boolean test(final String input) {
return Pattern.compile("[a-zA-Z]+").matcher(input).matches();
}
};
}
RegEx.ISLETTERS.test("abc");
单例模式
枚举是实现单例模式目前最好最优的方法
枚举被引用时才会实例化
public enum MySingletonPattern {
Loneliness;
private MySingletonPattern () {}
public String SomeWords() {
return "I am so lonely";
}
}
MySingletonPattern.Loneliness.SomeWords();
使用静态代码快
public enum EnglishWord {
a,b,c,d;
private String upper;
String getUpper() {
return upper;
}
static {
a.upper = "A";
b.upper = "B";
c.upper = "C";
d.upper = "D";
}
}
EnglishWord.a.getUpper();
EnumSet
EnumSet<EnglishWord> set = EnumSet.range(EnglishWord.a, EnglishWord.b); // 取枚举部分值from...to
EnumSet<EnglishWord> set = EnumSet.of(EnglishWord.a);
EnumMap
public enum EnglishLowerWord {
a, b, c;
}
public enum EnglishUpperWord {
A, B, C;
}
EnumMap<EnglishLowerWord, EnglishUpperWord> map = new EnumMap<EnglishLowerWord, EnglishUpperWord>(EnglishLowerWord.class);
map.put(EnglishLowerWord.a, EnglishUpperWord.A);
map.put(EnglishLowerWord.b, EnglishUpperWord.B);
map.put(EnglishLowerWord.c, EnglishUpperWord.C);
结语
本文章是java成神的系列文章之一
如果你想知道,但是本文没有的,请下方留言
我会第一时间总结出来并发布填充到本文
原文地址:https://www.cnblogs.com/ye-hcj/p/9745728.html
时间: 2024-09-28 05:27:55