Java 第七章.枚举类)
1.概述
1.类的对象是有限个的,确定的。我们称此类是枚举类
2.当需要定义一组常量时,强烈建议使用枚举类
3.如果枚举类中只有一个对象,则可以作为单例模式的实现方式。
2.如何定义枚举类
方式一:jdk5.0之前,自定义枚举类
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
public class SeasonTest {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
System.out.println(spring.toString());
System.out.println(spring.getSeasonName());
}
}
//自定义枚举类
class Season{
//1.声明Season的对象的属性,private final修饰
private final String seasonName;
private final String seasonDesc;
//2.私有化类的构造器,并给对象属性赋值
private Season(String seasonName,String seasonDesc){
this.seasonDesc = seasonDesc;
this.seasonName = seasonName;
}
//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.其他述求1.获取枚举类对象属性
public String getSeasonDesc() {
return seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
//4.其他述求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 spring = Season1.SPRING;
System.out.println(spring);
}
}
enum Season1{
//1.提供当前枚举类的多个对象
//格式: 对象名(形参列表),
//说明:每句结尾后面用,最后一个有;
SPRING("春天","春暖花开"),
SUMMER("夏天","热"),
AUTUMN("秋天","分手"),
WINTER("冬天","冷");
//2.声明Season的对象的属性,private final修饰
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器,并给对象属性赋值
private Season1(String seasonName,String seasonDesc){
this.seasonDesc = seasonDesc;
this.seasonName = seasonName;
}
//4.其他述求1.获取枚举类对象属性
public String getSeasonDesc() {
return seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
//4.其他述求1.提供toString()
@Override
public String toString() {
return "Season1{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
3.Enum类中的常用方法
1. values()方法:返回枚举类型的对象数组。该方法可以很方便地遍历所有的枚举值。
2. valueOf(String str):可以把一个字符串转为对应的枚举类对象。要求字符串必须是枚举类对象的“名字”。如不是,会有运行时异常:IllegalArgumentException。
(判断对象是否属于枚举类)
3. toString():返回当前枚举类对象常量的名称
应用
public class SeasonTest1 {
public static void main(String[] args) {
//toString()
Season1 spring = Season1.SPRING;
System.out.println(spring);
System.out.println("****************************");
//Values()
Season1[] val = Season1.values();
for (int i = 0; i < val.length;i++){
System.out.println(val[i]);
}
System.out.println("****************************");
//valuesOf(String str)
Season1 summer = Season1.valueOf("SUMMER");
System.out.println(summer);
}
}
enum Season1{
//1.提供当前枚举类的多个对象
//格式: 对象名(形参列表),
//说明:每句结尾后面用,最后一个有;
SPRING("春天","春暖花开"),
SUMMER("夏天","热"),
AUTUMN("秋天","分手"),
WINTER("冬天","冷");
//2.声明Season的对象的属性,private final修饰
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器,并给对象属性赋值
private Season1(String seasonName,String seasonDesc){
this.seasonDesc = seasonDesc;
this.seasonName = seasonName;
}
//4.其他述求1.获取枚举类对象属性
public String getSeasonDesc() {
return seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
}
4.使用enum关键字定义的枚举类实现接口的情况
情况一
实现接口,在enum类中实现抽象方法
情况二
让枚举类中的对象分别实现抽象方法
举例
public class SeasonTest1 {
public static void main(String[] args) {
//toString()
Season1 spring = Season1.SPRING;
System.out.println(spring);
System.out.println("****************************");
//Values()
Season1[] val = Season1.values();
for (int i = 0; i < val.length;i++){
System.out.println(val[i]);
val[i].show();
}
System.out.println("****************************");
//valuesOf(String str)
Season1 summer = Season1.valueOf("SUMMER");
System.out.println(summer);
spring.show();
}
}
interface info{
void show();
}
enum Season1{
//1.提供当前枚举类的多个对象
//格式: 对象名(形参列表),
//说明:每句结尾后面用,最后一个有;
//情况二
SPRING("春天","春暖花开"){
public void show(){
System.out.println("晴天");
}
},
SUMMER("夏天","热"){
public void show(){
System.out.println("夏天");
}
},
AUTUMN("秋天","分手"){
public void show(){
System.out.println("秋歌");
}
},
WINTER("冬天","冷"){
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.seasonDesc = seasonDesc;
this.seasonName = seasonName;
}
//4.其他述求1.获取枚举类对象属性
public String getSeasonDesc() {
return seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
//情况一
public void show(){
System.out.println("奥里给");
}
}