35 枚举类
作者:互联网
枚举类
类的对象只有有限个,确定的。举例如下:
星期:Monday(星期一)...Sunday(星期天
性别:Man(男)、Woman(女)
就职状态:Busy、Free、Vocation、Dimission
当需要定义一组常量时,强烈建议使用枚举类
如何定义枚举类
方式一:jdk5.0之前,自定义枚举类
public class SeasonTest {
public static void main(String[] args) {
Season autumn = Season.AUTUMN;
System.out.println(autumn);
}
}
//自定义枚举类
class Season{
//1.声明Season对象的属性:需要private、final属性修饰
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 的
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("冬天","冰天雪地");
//4.其他诉求:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//5.其他诉求1:提供toString()
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
方式二:jdk5.0,可以使用enum关键字定义枚举类
说明:定义的枚举类默认继承于java.lang.Enum类
public class SeasonTest1 {
public static void main(String[] args) {
Season1 summer = Season1.SUMMER;
System.out.println(summer);
}
}
//用enum关键字枚举类
enum Season1{
//1.当前枚举类的对象,多个对象之间用“,”隔开,末尾对象用“;”结束
SPRING("春天","春暖花开"),
SUMMER("秋天","秋高气爽"),
AUTUMN("夏天","夏日炎炎"),
Season1("冬天","冰天雪地");
//2.声明Season对象的属性:需要private、final属性修饰
private final String seasonName ;
private final String seasonDesc;
//3.私有化类的构造器
private Season1(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//4.其他诉求:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//5.其他诉求1:提供toString()
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
Enum类中的常用方法
values()
方法:返回枚举类型的对象数组。该方法可以很方便地遍历所有的枚举值
Season1[] values = Season1.values();
for(int i = 0;i < values.length;i++){
System.out.println(values[i]);
}
返回枚举类中对象名是str的对象
valueof(String str)
:可以把一个字符串转为对应的枚举类对象。要求字符串必须是枚举类对象的“名字”。如不是,会有运行时异常: llegalArgumentException。 toString()
:返回当前枚举类对象常量的名称
使用enum关键字定义的枚举类实现接口的情况
情况一:实现接口,在enum类中实现抽象方法
情况二:让枚举类的对象分别实现接口中的抽象方法
public class SeasonTest1 {
public static void main(String[] args) {
Season1 summer = Season1.SUMMER;
System.out.println(summer);
Season1[] values = Season1.values();
for(int i = 0;i < values.length;i++){
System.out.println(values[i]);
}
summer.show();
}
}
interface Info{
void show();
}
//用enum关键字枚举类
enum Season1 implements Info{
//1.当前枚举类的对象,多个对象之间用“,”隔开,末尾对象用“;”结束
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("大约在冬季");
}
};
//2.声明Season对象的属性:需要private、final属性修饰
private final String seasonName ;
private final String seasonDesc;
//3.私有化类的构造器
private Season1(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//4.其他诉求:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//5.其他诉求1:提供toString()
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
// @Override
// public void show() {
// System.out.println("这是个季节");
// }
}
标签:String,Season,枚举,35,seasonName,seasonDesc,public 来源: https://www.cnblogs.com/flypigggg/p/14613933.html