枚举
什么是枚举类?
实例(也叫对象)有限且固定不变的类,在Java里被称为枚举类。
什么时候用枚举类?
1.如何自定义枚举类
方式1(jdk5以前)
public class Zm {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);//Season{seasonName='春天', seasonDesc='1111'}
System.out.println(spring.getSeasonName() + spring.getSeasonDesc());//春天1111
}
}
class Season{
//1.声明Season对象的属性
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 Season SPRING = new Season("春天", "1111");
public static final Season SUMMER = new Season("夏天", "2222");
public static final Season AUTUMN = new Season("秋天", "3333");
//4.获取枚举类独享的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//5.把对象的属性输出一下
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
方式2(jdk5以后)
与jdk5以前不一样,基本表现在提供对象更简洁了,但是必须写在开头,而且默认重写了toStrng方法.
public class Zm {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);//Spring
System.out.println(spring.getSeasonName() + spring.getSeasonDesc());//春天1111
}
}
enum Season{
//1.提供枚举类的多个对象
/* public static final Season SPRING = new Season("春天", "1111");
public static final Season SUMMER = new Season("夏天", "2222");
public static final Season AUTUMN = new Season("秋天", "3333");*/
SPRING("春天", "1111"),
SUMMER("夏天", "2222"),
AUTUMN("秋天", "3333");
//2.声明Season对象的属性
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器,初始化赋值
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//4.获取枚举类独享的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//5.把对象的属性输出一下
/* @Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}*/
}
2.枚举类的主要方法
最重要的三个方法
public class Zm {
public static void main(String[] args) {
//values
System.out.println(Season.values());//[LSeason;@4554617c
Season[] values = Season.values();
for (int i = 0; i < Season.values().length ; i++) {
System.out.println(values[i]);//SPRING SUMMER AUTUMN
}
//valueof
Season spring = Season.valueOf("SPRING");
System.out.println(spring);//SPRING
//
Season spring1 = Season.SPRING;
System.out.println(spring1.toString());//SPRING
}
}
enum Season{
//1.提供枚举类的多个对象
/* public static final Season SPRING = new Season("春天", "1111");
public static final Season SUMMER = new Season("夏天", "2222");
public static final Season AUTUMN = new Season("秋天", "3333");*/
SPRING("春天", "1111"),
SUMMER("夏天", "2222"),
AUTUMN("秋天", "3333");
//2.声明Season对象的属性
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器,初始化赋值
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//4.获取枚举类独享的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//5.把对象的属性输出一下
/* @Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}*/
}
3.使用枚举类实现接口
方法一
public class Zm {
public static void main(String[] args) {
Season.SPRING.show();//好热
}
}
interface info{
public void show();
}
enum Season implements info{
//1.提供枚举类的多个对象
/* public static final Season SPRING = new Season("春天", "1111");
public static final Season SUMMER = new Season("夏天", "2222");
public static final Season AUTUMN = new Season("秋天", "3333");*/
SPRING("春天", "1111"),
SUMMER("夏天", "2222"),
AUTUMN("秋天", "3333");
//2.声明Season对象的属性
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器,初始化赋值
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//4.获取枚举类独享的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
@Override
public void show() {
System.out.println("好热");
}
//5.把对象的属性输出一下
/* @Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}*/
}
方法二
public class Zm {
public static void main(String[] args) {
Season[] values = Season.values();
for (int i = 0; i < values.length; i++) {
values[i].show();//好暖和 好热 好凉爽
}
}
}
interface info{
public void show();
}
enum Season implements info{
//1.提供枚举类的多个对象
/* public static final Season SPRING = new Season("春天", "1111");
public static final Season SUMMER = new Season("夏天", "2222");
public static final Season AUTUMN = new Season("秋天", "3333");*/
SPRING("春天", "1111"){
@Override
public void show() {
System.out.println("好暖和");
}
},
SUMMER("夏天", "2222"){
@Override
public void show() {
System.out.println("好热");
}
},
AUTUMN("秋天", "3333"){
@Override
public void show() {
System.out.println("好凉爽");
}
};
//2.声明Season对象的属性
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器,初始化赋值
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//4.获取枚举类独享的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//5.把对象的属性输出一下
/* @Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}*/
}