Enum学习
作者:互联网
/**
* @Auther: hs
* @Date: 2019/2/24 12:57
* @Description:私有构造,枚举单例
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public enum Day {
MONDAY("星期一"),
TUESDAY("星期二"),
WEDNESDAY("星期三"),
THURSDAY("星期四"),
FRIDAY("星期五"),
SATURDAY("星期六"),
SUNDAY("星期日");
private String desc;//中文描述
public static void main(String[] args) {
//遍历所有枚举获取name和值
for (Day day:Day.values()) {
System.out.println(day.name()+","+day.getDesc());
}
//调用name()方法,表示是string类型,valueof转成枚举
Day day = Day.valueOf(Day.MONDAY.name());
}
}
//------------------------------写法一-----------------------------------
/**
* @Auther: hs
* @Date: 2019/2/24 12:57
* @Description:与常规抽象类一样,enum类允许我们为其定义抽象方法,
* 然后使每个枚举实例都实现该方法,以便产生不同的行为方式,抽象方法放在最后
*/
public interface Day {
enum DayType{
MONDAY{
@Override
public String getInfo() {
return "周一";
}
},
TUESDAY{
@Override
public String getInfo() {
return "周二";
}
};
public abstract String getInfo();
}
public static void main(String[] args) {
System.out.println(DayType.MONDAY.getInfo());
}
}
//-------------------------------写法二-----------------------------------
/**
* @Auther: hs
* @Date: 2019/2/24 12:57
* @Description: 接口中定义抽象方法,与方法一类似
*/
public interface Day {
String getInfo();
enum DayType implements Day{
MONDAY {
@Override
public String getInfo() {
return "周一";
}
},
TUESDAY{
@Override
public String getInfo() {
return "周二";
}
};
}
public static void main(String[] args) {
System.out.println(DayType.MONDAY.getInfo());
}
}
package com.staryea.jsondemo.test;
/**
* @Auther: hs
* @Date: 2019/2/24 12:57
* @Description:
*/
public interface Food {
String getInfo();
enum Appetizer implements Food {
SALAD, SOUP, SPRING_ROLLS;
@Override
public String getInfo() {
return null;
}
}
enum MainCourse implements Food {
LASAGNE, BURRITO, PAD_THAI,
LENTILS, HUMMOUS, VINDALOO;
@Override
public String getInfo() {
return null;
}
}
enum Dessert implements Food {
TIRAMISU, GELATO, BLACK_FOREST_CAKE,
FRUIT, CREME_CARAMEL;
@Override
public String getInfo() {
return null;
}
}
enum Coffee implements Food {
BLACK_COFFEE, DECAF_COFFEE, ESPRESSO,
LATTE, CAPPUCCINO, TEA, HERB_TEA;
@Override
public String getInfo() {
return CAPPUCCINO.name();
}
}
}
标签:enum,return,String,getInfo,Enum,public,学习,Day 来源: https://blog.csdn.net/qq_26869339/article/details/87903995