主要内容:
- 星期:Monday(星期一)、......、Sunday(星期天)
- 性别:Man(男)、Woman(女)
- 季节:Spring(春节)......Winter(冬天)
- 支付方式:Cash(现金)、WeChatPay(微信)、Alipay(支付宝)、BankCard(银 行卡)、CreditCard(信用卡)
- 就职状态:Busy、Free、Vocation、Dimission
- 订单状态:Nonpayment(未付款)、Paid(已付款)、Delivered(已发货)、 Return(退货)、Checked(已确认)Fulfilled(已配货)、
- 线程状态:创建、就绪、运行、阻塞、死亡
当需要定义一组常量时,强烈建议使用枚举类
枚举类的实现
若枚举只有一个对象, 则可以作为一种单例模式的实现方式
枚举类的属性
public class SeasonTest {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
}
}
//自定义枚举类
class Season{
//1.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//2.私有化类的构造器,并给对象属性赋值
private Season(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//3.提供当前枚举类的多个对象:public static final的
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("冬天","冰天雪地");
//4.其他要求1:可以获取枚举类对象的属性(提供getter方法)
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//4.其他要求2:可以提供toString()方法
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
/**
* 使用enum关键字定义枚举类
* 说明:定义的枚举类默认继承于java.lang.Enum
*/
public class SeasonTest1 {
public static void main(String[] args) {
Season1 summer = Season1.SUMMER;
//toString();
System.out.println(summer.toString());
System.out.println(Season1.class.getSuperclass());
}
}
//使用enum关键字定义枚举类
enum Season1{
//1.提供当前枚举类的对象,多个对象之间用逗号隔开,末尾用分号结束
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
//2.声明Season1对象的属性:private final修饰
private final String Season1Name;
private final String Season1Desc;
//3.私有化类的构造器,并给对象属性赋值
private Season1(String Season1Name,String Season1Desc){
this.Season1Name = Season1Name;
this.Season1Desc = Season1Desc;
}
//可以获取枚举类对象的属性(提供getter方法)
public String getSeason1Name() {
return Season1Name;
}
public String getSeason1Desc() {
return Season1Desc;
}
enum枚举类实现接口的情况:
情况一:实现接口,在enum类中实现抽象方法
interface Info{
void show();
}
//使用enum关键字定义枚举类
enum Season1 implements Info{
...此处省略的代码和上面的一样...
@Override
public void show() {
System.out.println("这是一个季节");
}
}
情况二:每个枚举类的对象都实现接口的抽象方法,在不同对象里实现不同操作
public class SeasonTest1 {
public static void main(String[] args) {
Season1[] values = Season1.values();
for (Season1 item : values) {
System.out.println(item);
item.show();
System.out.println("***********");
}
}
}
interface Info{
void show();
}
//使用enum关键字定义枚举类
enum Season1 implements Info{
//1.提供当前枚举类的对象,多个对象之间用逗号隔开,末尾用分号结束
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("现在是冬天");
}
};
//2.声明Season1对象的属性:private final修饰
private final String Season1Name;
private final String Season1Desc;
//3.私有化类的构造器,并给对象属性赋值
private Season1(String Season1Name,String Season1Desc){
this.Season1Name = Season1Name;
this.Season1Desc = Season1Desc;
}
//可以获取枚举类对象的属性(提供getter方法)
public String getSeason1Name() {
return Season1Name;
}
public String getSeason1Desc() {
return Season1Desc;
}
@Override
public void show() {
System.out.println("这是一个季节");
}
}
运行结果:
SPRING
现在是春天
***********
SUMMER
现在是夏天
***********
AUTUMN
现在是秋天
***********
WINTER
现在是冬天
***********