EnumMap demo

优点:常量做为Key,在编译期就确定了。Enum做为key,在运行时也可以改变

package enumdemo;

import java.util.EnumMap;
import java.util.Map;
import java.util.Set; public class EnumMaps {
public static void main(String[] args) {
EnumMap<AlarmPoints, Command> em = new EnumMap<AlarmPoints, Command>(AlarmPoints.class);
em.put(AlarmPoints.KITCHEN, new Command() {
@Override
public void action() {
System.out.println("action:kitchen...");
}
});
em.put(AlarmPoints.BATHROOM, new Command() {
@Override
public void action() {
System.out.println("action:bathroom");
}
}); Set<Map.Entry<AlarmPoints, Command>> set = em.entrySet();
for (Map.Entry<AlarmPoints, Command> entry : set) {
System.out.println(entry.getKey() + "");
entry.getValue().action();
} System.out.println(em.get(AlarmPoints.UTILITY) + ""); }
} enum AlarmPoints {
KITCHEN,
BATHROOM,
UTILITY
} interface Command {
void action();
}
上一篇:C杂记


下一篇:照相、从相册上取照片、播放音频、播放本地视频、播放网络视频、MPMoviePlayerController