文章目录
前言
使用场景:
当变量只能从一堆固定的值中取出一个时,那么就应该使用枚举。
如:日期,学习,职业等等
枚举类 在jdk1.5 新增的一个关键字 在java5之前使用的是自定义 5之后就开始使用enum了
所以本文就介绍两种方法:
自定义枚举类
class TestDay{
//枚举类的属性 用private 修饰
private final String time;
private final String desc;
//参数用private 修饰构造函数 防止其他的类再去new
private TestDay(String time, String desc) {
this.time = time;
this.desc = desc;
}
//提供枚举的对象 给别的类使用 用publice 进行修饰
public static final TestDay morning=new TestDay("早上","早上要早起");
public static final TestDay afternoon =new TestDay("中午","中午要午睡");
public static final TestDay evening =new TestDay