一、枚举类型的使用规范
阿里巴巴Java开发手册中对enum类型的使用说明:
对enum类的命名说明:
对枚举类型的使用限制:
对枚举类型的强制注释:
二、通过demo认识枚举
Season.java
一个包含枚举的普通类
package com.jake.guidelines.enums;
public class Season {
SeasonEnums seasonEnum;
public enum SeasonEnums {
SPRING(1), SUMMER(2), AUTUMN(3), WINTER(4);
private int seq;
SeasonEnums(int seq){
this.seq = seq;
System.out.println("The season-enum constructor is invoked, and the seq = " + seq);
}
}
public static void printSeasonVerses(SeasonEnums seasonEnums) {
switch (seasonEnums) {
case SPRING:
System.out.println("春风又绿江南岸");
break;
case SUMMER:
System.out.println("但惜夏日长");
break;
case AUTUMN:
System.out.println("天气晚来秋");
break;
case WINTER:
System.out.println("疑是经冬雪未销");
break;
}
}
}
SeasonEnumTest.java
枚举测试类
package com.jake.guidelines.enums;
import org.junit.Test;
public class SeasonEnumTest {
@Test
public void testSeasonEnum() {
System.out.println(Season.SeasonEnums.SPRING);
}
@Test
public void testSeasonEnumValue() {
System.out.println(Season.SeasonEnums.valueOf("SPRING"));
}
@Test
public void testSeasonEnumValues() {
for (Season.SeasonEnums season : Season.SeasonEnums.values()) {
System.out.println(season.name());
}
}
@Test
public void testSeasonEnumOrdinate() {
Season.SeasonEnums winter = Season.SeasonEnums.WINTER;
System.out.println(winter.ordinal());
}
@Test
public void testSeasonEnumField() {
Season season = new Season();
season.seasonEnum = Season.SeasonEnums.SUMMER;
System.out.println(season.seasonEnum.name());
}
@Test
public void testSeasonEnumsInSwitchCases() {
Season.printSeasonVerses(Season.SeasonEnums.SPRING);
}
}
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/19610.html