枚举类型
枚举类型
类的对象只有有限个,确定的,我们就可以称之为枚举
星期:Monday(星期一).....Sunday(星期天)
性别:Man(男),Woman(女)
季节:Spring(春天)...winter(冬天)
就职状态。。。
当需要定义一组常量的时候,强烈建议使用枚举
JDK1.5之前:自定义枚举
/*
枚举
JDK1.5之前使用自定义枚举
*/
public class EnumDemo1 {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
}
}
class Season{
//2、创建枚举类的属性(成员变量),必须是作为私有常量出现
private final String SEASON_NAME;
private final String SEASON_DESC;
//1、必须将构造方法私有化,这是为了保证类的对象是有限个的目的
private Season(String SEASON_NAME,String SEASON_DESC){
this.SEASON_NAME = SEASON_NAME;
this.SEASON_DESC = SEASON_DESC;
}
//3、提供公共的静态的自定义常量给外界获取枚举类中多个对象
public static final Season SPRING = new Season("春天", "春暖花开");
public static final Season SUMMER = new Season("夏天", "夏日炎炎");
public static final Season FALL = new Season("秋天", "秋高气爽");
public static final Season WINTER = new Season("冬天", "白雪皑皑");
//4、提供公共的获取属性的方法
public String getSEASON_NAME() {
return SEASON_NAME;
}
public String getSEASON_DESC() {
return SEASON_DESC;
}
//5、重写toString()方法
@Override
public String toString() {
return "Season{" +
"SEASON_NAME='" + SEASON_NAME + '\'' +
", SEASON_DESC='" + SEASON_DESC + '\'' +
'}';
}
}
JDK1.5之后:通过关键字enum定义枚举类
/*
枚举
JDK1.5之前使用自定义枚举
JDK1.5之后:通过关键字enum定义枚举类
*/
public class EnumDemo2 {
public static void main(String[] args) {
Season2 spring = Season2.SPRING;
//将自己重写的toString()方法注释掉之后
//发现输出结果并不是地址值--该类肯定有重写的toString()方法
System.out.println(spring);// SPRING
//所有的枚举类都有个父类,叫做Enum
//所以Enum中肯定重写了toString()方法
System.out.println(Season2.class.getSuperclass());
System.out.println(spring.getSEASON_NAME()+"---"+spring.getSEASON_DESC());
}
}
enum Season2{
//3、提供公共的静态的自定义常量给外界获取枚举类中多个对象
//将枚举相关的对象放在开头,并用 ,分隔
SPRING("春天", "春暖花开"),
SUMMER("夏天", "夏日炎炎"),
FALL("秋天", "秋高气爽"),
WINTER("冬天", "白雪皑皑");
//2、创建枚举类的属性(成员变量),必须是作为私有常量出现
private final String SEASON_NAME;
private final String SEASON_DESC;
//1、必须将构造方法私有化,这是为了保证类的对象是有限个的目的
private Season2(String SEASON_NAME,String SEASON_DESC){
this.SEASON_NAME = SEASON_NAME;
this.SEASON_DESC = SEASON_DESC;
}
//4、提供公共的获取属性的方法
public String getSEASON_NAME() {
return SEASON_NAME;
}
public String getSEASON_DESC() {
return SEASON_DESC;
}
//5、重写toString()方法
// @Override
// public String toString() {
// return "Season{" +
// "SEASON_NAME='" + SEASON_NAME + '\'' +
// ", SEASON_DESC='" + SEASON_DESC + '\'' +
// '}';
// }
}
枚举类可以实现接口
1、直接在枚举类实现接口中的抽象方法
2、在每个枚举对象中实现
/*
1、直接在枚举类实现接口中的抽象方法
*/
public class EnumDemo3 {
public static void main(String[] args) {
Season3 spring = Season3.SPRING;
System.out.println(spring);// SPRING
//所有的枚举类都有个父类,叫做Enum
System.out.println(Season3.class.getSuperclass());
System.out.println(spring.getSEASON_NAME()+"---"+spring.getSEASON_DESC());
//直接在枚举类实现接口中的抽象方法有个缺点:
//每一个季节都是实现同一个show()方法
//那有些情况下,每一个对象实现的show()方法可能不太一样,那怎么办呢?
//在每个枚举对象中实现即可
spring.show();
}
}
interface Person{
void show();
}
enum Season3 implements Person{
//3、提供公共的静态的自定义常量给外界获取枚举类中多个对象
//将枚举相关的对象放在开头
SPRING("春天", "春暖花开"),
SUMMER("夏天", "夏日炎炎"),
FALL("秋天", "秋高气爽"),
WINTER("冬天", "白雪皑皑");
//2、创建枚举类的属性(成员变量),必须是作为私有常量出现
private final String SEASON_NAME;
private final String SEASON_DESC;
//1、必须将构造方法私有化,这是为了保证类的对象是有限个的目的
private Season3(String SEASON_NAME,String SEASON_DESC){
this.SEASON_NAME = SEASON_NAME;
this.SEASON_DESC = SEASON_DESC;
}
//4、提供公共的获取属性的方法
public String getSEASON_NAME() {
return SEASON_NAME;
}
public String getSEASON_DESC() {
return SEASON_DESC;
}
//在枚举类实现接口中的抽象方法
@Override
public void show() {
System.out.println("好好学习天天向上");
}
//5、重写toString()方法
// @Override
// public String toString() {
// return "Season{" +
// "SEASON_NAME='" + SEASON_NAME + '\'' +
// ", SEASON_DESC='" + SEASON_DESC + '\'' +
// '}';
// }
}
/*
2、在每个枚举对象中实现
*/
public class EnumDemo4 {
public static void main(String[] args) {
Season4 spring = Season4.SPRING;
System.out.println(spring);// SPRING
//所有的枚举类都有个父类,叫做Enum
System.out.println(Season4.class.getSuperclass());
System.out.println(spring.getSEASON_NAME()+"---"+spring.getSEASON_DESC());
spring.show();//-出去春游
Season4 winter = Season4.WINTER;
winter.show();//-冬天打雪仗
}
}
interface Person2{
void show();
}
enum Season4 implements Person2{
//4、提供公共的静态的自定义常量给外界获取枚举类中多个对象
//将枚举相关的对象放在开头
//在每个枚举对象后面用大括号实现重写的方法,让每个枚举对象都有对应的show()方法去实现。
SPRING("春天", "春暖花开"){
@Override
public void show() {
System.out.println("出去春游");
}
},
SUMMER("夏天", "夏日炎炎"){
@Override
public void show() {
System.out.println("夏天吃西瓜");
}
},
FALL("秋天", "秋高气爽"){
@Override
public void show() {
System.out.println("秋天中秋吃月饼");
}
},
WINTER("冬天", "白雪皑皑"){
@Override
public void show() {
System.out.println("冬天打雪仗");
}
};
//2、创建枚举类的属性(成员变量),必须是作为私有常量出现
private final String SEASON_NAME;
private final String SEASON_DESC;
//1、必须将构造方法私有化,这是为了保证类的对象是有限个的目的
private Season4(String SEASON_NAME,String SEASON_DESC){
this.SEASON_NAME = SEASON_NAME;
this.SEASON_DESC = SEASON_DESC;
}
//4、提供公共的获取属性的方法
public String getSEASON_NAME() {
return SEASON_NAME;
}
public String getSEASON_DESC() {
return SEASON_DESC;
}
// @Override
// public void show() {
// System.out.println("好好学习天天向上");
// }
//5、重写toString()方法
// @Override
// public String toString() {
// return "Season{" +
// "SEASON_NAME='" + SEASON_NAME + '\'' +
// ", SEASON_DESC='" + SEASON_DESC + '\'' +
// '}';
// }
}