自定义枚举类
package com.yuzhenc.enums;
public class Test01 {
public static void main(String[] args) {
Season season = Season.AUTUMN;
System.out.println(season);
System.out.println(season.getSeasonName());
System.out.println(season.getSeasonDesc());
}
}
class Season {
private final String seasonName ;
private final String seasonDesc ;
private Season(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public static final Season SPRING = new Season("春天","春暖花开");
public static final Season SUMMER = new Season("夏天","烈日炎炎");
public static final Season AUTUMN = new Season("秋天","硕果累累");
public static final Season WINTER = new Season("冬天","冰天雪地");
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
enum关键字
package com.yuzhenc.enums;
public class Test02 {
public static void main(String[] args) {
Season1 season1 = Season1.AUTUMN;
System.out.println(season1);
System.out.println(season1.getSeasonName());
System.out.println(season1.getSeasonDesc());
}
}
enum Season1 {
SPRING("春天","春暖花开"),
SUMMER("夏天","烈日炎炎"),
AUTUMN("秋天","硕果累累"),
WINTER("冬天","冰天雪地");
private final String seasonName ;
private final String seasonDesc ;
private Season1(String seasonName, String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
@Override
public String toString() {
return "Season1{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
常用方法
package com.yuzhenc.enums;
public class Test03 {
public static void main(String[] args) {
Season1 season1 = Season1.SPRING;
System.out.println(season1);
Season1[] season1s = Season1.values();
for (Season1 season11:season1s) {
System.out.println(season11);
}
Season1 season11 = Season1.valueOf("SPRING");
System.out.println(season11);
}
}
枚举类实现接口
package com.yuzhenc.enums;
public class Test04 {
public static void main(String[] args) {
Season2 spring = Season2.SPRING;
spring.show();
Season2 summer = Season2.SUMMER;
summer.show();
}
}
interface TestInterface {
void show();
}
enum Season2 implements TestInterface{
SPRING,
SUMMER,
AUTUMN,
WINTER;
@Override
public void show(){
System.out.println("这是Season2....");
}
}
package com.yuzhenc.enums;
public class Test05 {
public static void main(String[] args) {
Season3 spring = Season3.SPRING;
spring.show();
Season3 summer = Season3.SUMMER;
summer.show();
}
}
enum Season3 implements TestInterface {
SPRING{
@Override
public void show() {
System.out.println("这是春天");
}
},
SUMMER{
@Override
public void show() {
System.out.println("这是夏天");
}
},
AUTUMN{
@Override
public void show() {
System.out.println("这是秋天");
}
},
WINTER{
@Override
public void show() {
System.out.println("这是冬天");
}
};
}
枚举结合switch
package com.yuzhenc.enums;
public class Test06 {
public static void main(String[] args) {
Gender sex = Gender.男;
switch (sex){
case 女:
System.out.println("是个女孩");
break;
case 男:
System.out.println("是个男孩");
break;
}
}
}
enum Gender {
男,
女;
}