1、基本概念
1、当一个类的对象只有有限个,确定个时,我们把这个类称作枚举类。 2、当需要定义一组常量时,强烈建议使用枚举类
3、枚举类的实现
2、自定义枚举类
package com.example.java;
class seasonTest01 {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
}
}
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 + '\'' +
'}';
}
}
3、使用enum关键字定义枚举类
package com.example.java;
public class SeasonTest02 {
public static void main(String[] args) {
Season2 spring = Season2.SPRING;
System.out.println(spring);
System.out.println(Season2.class.getSuperclass());
}
}
enum Season2 {
SPRING("春天","吹暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
private final String seasonName;
private final String seasonDesc;
private Season2(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
4、枚举类的主要方法
4.1、枚举类的常用方法
package com.example.java;
public class SeasonTest02 {
public static void main(String[] args) {
System.out.println("*************toString();**************");
Season2 spring = Season2.SPRING;
System.out.println(spring);
System.out.println("*************values()**************");
Season2[] values = Season2.values();
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
System.out.println("*************valueOf(String objName)**************");
Season2 winter = Season2.valueOf("WINTER");
System.out.println(winter);
}
}
enum Season2 {
SPRING("春天","吹暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
private final String seasonName;
private final String seasonDesc;
private Season2(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
5、枚举类实现接口
package com.example.java;
import java.lang.ref.SoftReference;
import java.sql.SQLOutput;
public class SeasonTest02 {
public static void main(String[] args) {
System.out.println("*************values()**************");
Season2[] values = Season2.values();
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
values[i].show();
}
}
}
interface info{
void show();
}
enum Season2 implements info{
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("这是冬季");
}
};
private final String seasonName;
private final String seasonDesc;
private Season2(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
|