枚举(enumeration)
-
枚举的二种类实现方式:
(1)自定义类实现枚举
(2)使用enum关键字实现枚举
-
自定义枚举 应用举例:
(1)不需要提供set方法,不允许修改操作,因为枚举对象值通常为只读
(2)对枚举对象/属性使用final+static共同修饰,实现底层优化
(3)枚举对象名通常使用全部大写,命名规范与常量相同
(4)枚举对象根据需要,也可有多个属性
public class Test {
public static void main(String[] args){
System.out.println(Season.SPRING);
System.out.println(Season.SUMMER);
System.out.println(Season.AUTTUM);
System.out.println(Season.WINTER);
}
}
class Season{
private String name;
private String feel;
//本部内部创建一组对象,并对外暴露对象
public static final Season SPRING = new Season("春天","温暖");
public static final Season SUMMER = new Season("夏天","炎热");
public static final Season AUTTUM = new Season("秋天","凉爽");
public static final Season WINTER = new Season("冬天","寒冷");
//构造器私有化,防止属性被修改
private Season(String name, String feel) {
this.name = name;
this.feel = feel;
}
//只提供get方法,不提供set方法
public String getName() {
return name;
}
public String getFeel() {
return feel;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", feel='" + feel + '\'' +
'}';
}
}
-
enum实现枚举应用实例
上面的例子中,如果使用enum实现枚举,只需将class替换为enum,内部稍作调整即可
public class Test {
public static void main(String[] args){
System.out.println(Season.SPRING);
System.out.println(Season.SUMMER);
System.out.println(Season.AUTTUM);
System.out.println(Season.WINTER);
}
}
enum Season{
SPRING("春天","温暖"),SUMMER("夏天","炎热"),AUTTUM("秋天","凉爽"),WINTER("冬天","寒冷");
private String name;
private String feel;
//防止属性被修改
private Season(String name, String feel) {
this.name = name;
this.feel = feel;
}
public String getName() {
return name;
}
public String getFeel() {
return feel;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", feel='" + feel + '\'' +
'}';
}
}
细节和注意事项
(1)当我们使用 enum 关键字开发一个枚举类时,默认会继承Enum类,而且是一个final类
(2)传统的 public static final Season SPRING = new Season("春天","温暖"),简化成SPRING("春天","温暖"),须知它对应的构造器
(3)如果使用无参构造器创建枚举对象,则实参列表和小括号都可以省略
(4)当有多个枚举对象时,使用" , "间隔,最后有一个分号结尾
(5)枚举对象必须放在枚举类的行首
-
enum常用方法举例:
public class Test {
public static void main(String[] args){
//使用Season枚举类,演示enum的几种方法
Season autumn = Season.AUTTUM;
//输出枚举对象的名字
System.out.println(autumn.name());
//输出枚举对象的次序(从0开始)
System.out.println(autumn.ordinal());
System.out.println();
//遍历取出Season中的常量
Season[] values = Season.values();
for(Season season : values){
System.out.println(season);
}
//valueof:根据你输入的值,到Season类中查找
//如果找到了就返回,如果没有找到,就报错
Season auttum1 = Season.valueOf("AUTTUM");
System.out.println(autumn == auttum1);
}
}
enum Season{
SPRING("春天","温暖"),SUMMER("夏天","炎热"),AUTTUM("秋天","凉爽"),WINTER("冬天","寒冷");
private String name;
private String feel;
//防止属性被修改
private Season(String name, String feel) {
this.name = name;
this.feel = feel;
}
public String getName() {
return name;
}
public String getFeel() {
return feel;
}
}
-
例子
(1)声明Week枚举类,其中包含星期一至星期日的定义;
(2)使用values返回所有的枚举数组,并遍历
public class Test {
public static void main(String[] args) {
Week[] values = Week.values();
for(Week week : values){
System.out.println(week.getChina()+" "+week.getName()+" "+week.getNum());
}
}
}
enum Week{
MONDAY("星期一","马原",2),TUESDAY("星期二","组成原理",6),WEDNESDAY("星期三","操作系统",5),THURSDAY("星期四","离散数学",3),FRIDAY("星期五","线性代数",4),SATURDAY("星期六","数据结构",6),SUNDAY("星期日","存储优化",4);
private String name;
private int num;
private String china;
Week(String china,String name, int num) {
this.name = name;
this.num = num;
this.china = china;
}
public String getName() {
return name;
}
public int getNum() {
return num;
}
public String getChina() {
return china;
}
}
-
细节和注意事项
(1)使用enum关键字后,就不能再继承其它类了,因为enum会隐式继承Enum,而Java是单继承机制
(2)枚举类和普通类一样,可以实现接口
public class Test {
public static void main(String[] args) {
Song.KARRY.listen();
}
}
enum Song implements music{
KARRY;
public void listen(){
System.out.println("树读正在播放...");
}
}
interface music{
public void listen();
}