Java|java枚举enumeration

Java|java枚举enumeration

/**
* 测试枚举enum
*/
enum EnumSeason {
//enum默认extends java.lang.Enum  本质也是类
SPRINGSUMMERAUTUMNWINTER
//在枚举内创建常量 用隔开 不赋值

enum EnumWeek{
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSATURDAYSUNDAY
//每个成员默认public static final修饰 常量

class TestEnum{
public static void main(String[
args) {
System.out.println(EnumSeason.SPRING);
//通过枚举名. 直接调用 结果为SPRING
System.out.println(Arrays.toString(EnumSeason.values()));
//枚举名.values()返回常量数组 数组的类型是Season[
每一个元素都是Season的实例 结果[SPRING SUMMER AUTUMN WINTER

for (int i = 0; i< EnumSeason.values().length; i++){
System.out.println(EnumSeason.values()[i
);

for (EnumSeason s: EnumSeason.values()){
//for-each要输入对应的类型  .values()返回的是 Season[
数组 所以引用变量s也要用Season
System.out.println(s);
//常量本身不是字符串 enum重写了.toString 让常量打印自己的名字

int dayOfWeek = new Random().nextInt(EnumWeek.values().length);
switch (EnumWeek.values()[dayOfWeek
){
case MONDAY:
//不可以写EnumWeek.MONDAY
System.out.println(1);
break;
case TUESDAY:
System.out.println(2);
break;
case WEDNESDAY:
System.out.println(3);
break;
case THURSDAY:
System.out.println(4);
break;
case FRIDAY:
System.out.println(5);
break;
case SATURDAY:
System.out.println(6);
break;
case SUNDAY:
System.out.println(7);
【Java|java枚举enumeration】break;