如何定义枚举类
什么枚举类:类里的对象是有限个,可以枚举出来
如何使用enum定义枚举类
枚举类的主要方法
实现接口的枚举类
若枚举只有一个成员,则可以看作为一种单利模式的实现方式。
----------------------------------------------------------------------
public classTestSeason {
public static voidmain(String[] args) {
Season spring = Season.SPRING;
System.out.println("-_-_-_-");
spring.show();
System.out.println("-_-_-_-");
System.out.println(spring);
}
}
//自定义枚举类
classSeason{
private finalStringseasonName;
private finalStringseasonDesc;
privateSeason(String seasonName,String seasonDesc) {
this.seasonName= seasonName;
this.seasonDesc= seasonDesc;
}
// 称为枚举类的原因,在类的内部确定好,外部能做的就是一个调用
//
public static finalSeasonSPRING=newSeason("春天Spring","春暖花开");
public static finalSeasonSUMMER=newSeason("夏天Summer","烈日炎炎");
public static finalSeasonAUTOM=newSeason("秋天Autom","秋高气爽");
public static finalSeasonWINTER=newSeason("冬天Winter","白雪皑皑");
publicStringgetSeasonName() {
returnseasonName;
}
publicStringgetSeasonDesc() {
returnseasonDesc;
}
@Override
publicStringtoString() {
return"Season{"+
"seasonName='"+seasonName+'\''+
", seasonDesc='"+seasonDesc+'\''+
'}';
}
public voidshow(){
System.out.println("show season");
}
}
-------------------------------------------------------------
/**
* Created by daqingtian on 2017/8/3.
* 枚举类
*/
public classTestSeason1 {
public static voidmain(String[] args) {
Season1 spring = Season1.SAPRING;
System.out.println(spring);
//1 values 返回season 的所有枚举对象
Season1[] seasons = Season1.values();
for(inti =0;i
System.out.println(seasons[i]);
}
//valueOf(String name)要求传入的形参是枚举对象的名字;
//Exception in thread "main" java.lang.IllegalArgumentException:
// No enum constant charact9.Season1.SPRING
//如果传入的枚举对象错误,会出现那样的错误。
String str ="SAPRING";
Season1 season = Season1.valueOf(str);
System.out.println(season);
}
}
enumSeason1{
SAPRING("春天Spring","春暖花开"),
SUMMER("夏天Summer","烈日炎炎"),
AUTOM("秋天Autom","秋高气爽"),
WINTER("冬天Winter","白雪皑皑");
private finalStringseasonName;
private finalStringseasonDesc;
privateSeason1(String seasonName,String seasonDesc) {
this.seasonName= seasonName;
this.seasonDesc= seasonDesc;
}
publicStringgetSeasonName() {
returnseasonName;
}
publicStringgetSeasonDesc() {
returnseasonDesc;
}
}
------------------------------------------------------
/**
* Created by daqingtian on 2017/8/3.
* 枚举类实现接口
*/
public classTestSeason3 {
public static voidmain(String[] args) {
Season3 spring = Season3.SAPRING;
// System.out.println(spring);
//1 values 返回season 的所有枚举对象
Season3[] seasons = Season3.values();
for(inti =0;i
// System.out.println(seasons[i]);
}
//valueOf(String name)要求传入的形参是枚举对象的名字;
//Exception in thread "main" java.lang.IllegalArgumentException:
// No enum constant charact9.Season1.SPRING
//如果传入的枚举对象错误,会出现那样的错误。
String str ="SAPRING";
Season3 season = Season3.valueOf(str);
season.show();
}
}
interfaceInfo{
voidshow();
}
enumSeason3implementsInfo{
SAPRING("春天Spring","春暖花开") {
@Override
public voidshow() {
System.out.println("春天来了");
}
},
SUMMER("夏天Summer","烈日炎炎") {
@Override
public voidshow() {
System.out.println("夏天来了");
}
},
AUTOM("秋天Autom","秋高气爽") {
@Override
public voidshow() {
System.out.println("秋天来了");
}
},
WINTER("冬天Winter","白雪皑皑") {
@Override
public voidshow() {
System.out.println("冬天来了");
}
};
private finalStringseasonName;
private finalStringseasonDesc;
privateSeason3(String seasonName,String seasonDesc) {
this.seasonName= seasonName;
this.seasonDesc= seasonDesc;
}
publicStringgetSeasonName() {
returnseasonName;
}
publicStringgetSeasonDesc() {
returnseasonDesc;
}
// public void show(){
// System.out.println("show season");
// }
}