枚举类概述
在某些情况下,一个类的对象是有限而且固定的。例如季节类,只能有 4 个对象
手动实现枚举类:
- private 修饰构造器
- 属性使用 private final 修饰
- 把该类的所有实例都使用 public static final 来修饰
Season.java
package com.xyc.enums;
/**
*
* @ClassName: Season
* @Description:自定义枚举
* @author: xyc
* @date: 2017年3月20日 下午9:43:11
* 1.属性使用 private final 修饰
* 2.private 修饰构造器
* 3.把该类的所有实例都使用 public static final 来修饰
*/
public class Season {
//属性是固定的,说明属性是常量
private final String SEASON_NAME;
private final String SEASON_DESC;
//在类的外部不能创建多个对象,所以私有化构造器
private Season(String seasonName, String seasonDesc) {
SEASON_NAME = seasonName;
SEASON_DESC = seasonDesc;
}
//在类的内部创建4个对象,且这四个对象不能被修改
public final static Season SPRING = new Season("春天", "春风又绿江南岸");
public final static Season SUMMER = new Season("夏天", "映日荷花别样红");
public final static Season AUTUMN = new Season("秋天", "秋水共长天一色");
public final static Season WINTER = new Season("冬天", "窗含西岭千秋雪");
public String getSEASON_NAME() {
return SEASON_NAME;
}
public String getSEASON_DESC() {
return SEASON_DESC;
}
@Override
public String toString() {
return "Season [SEASON_NAME=" + SEASON_NAME + ", SEASON_DESC="
+ SEASON_DESC + "]";
}
}
@Test
public void testSeason() {
Season se = Season.AUTUMN;
System.err.println(se);
//Season [SEASON_NAME=秋天, SEASON_DESC=秋水共长天一色]
}
使用 enum 定义枚举类
JDK 1.5 新增的 enum 关键字用于定义枚举类
枚举类和普通类的区别:
- 使用 enum 定义的枚举类默认继承了 java.lang.Enum 类
- 枚举类的构造器只能使用 private 访问控制符
- 枚举类的所有实例必须在枚举类中显式列出(, 分隔 ; 结尾). 列出的实例系统会自动添加 public static final 修饰
- 所有的枚举类都提供了一个 values 方法, 该方法可以很方便地遍历所有的枚举值
枚举类的属性
- 枚举类对象的属性不应允许被改动, 所以应该使用 private final 修饰
- 枚举类使用 private final 修饰的属性应该在构造器中为其赋值
- 若枚举类显式的定义了带参数的构造器, 则在列出枚举值时也必须对应的传入参数
枚举类常量的使用
public enum Color {
RED, GREEN, BLANK, YELLOW
}
使用
public class B {
public static void main(String[] args) {
System.out.println( isRed( Color.BLANK ) ) ; //结果: false
System.out.println( isRed( Color.RED ) ) ; //结果: true
}
static boolean isRed( Color color ){
if ( Color.RED.equals( color )) {
return true ;
}
return false ;
}
}
或者 switch 的使用
public class B {
public static void main(String[] args) {
showColor( Color.RED );
}
static void showColor(Color color){
switch ( color ) {
case BLANK:
System.out.println( color );
break;
case RED :
System.out.println( color );
break;
default:
System.out.println( color );
break;
}
}
}
枚举类自定义函数
public enum Color {
RED("红色", 1),
GREEN("绿色", 2),
BLANK("白色", 3),
YELLO("黄色", 4);
private String name ;
private int index ;
private Color( String name , int index ){
this.name = name ;
this.index = index ;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
//使用
public class B {
public static void main(String[] args) {
//输出某一枚举的值
System.out.println( Color.RED.getName() );
System.out.println( Color.RED.getIndex() );
//遍历所有的枚举
for( Color color : Color.values()){
System.out.println( color + " name: " + color.getName() + " index: " + color.getIndex() );
}
}
}
实现接口的枚举类
- 和普通 Java 类一样枚举类可以实现一个或多个接口
- 若需要每个枚举值在调用实现的接口方法呈现出不同的行为方式, 则可以让每个枚举值分别来实现该方法
public interface Info {
public String getInfo();
}
//第一种方式实现接口
public enum Color2 implements Info {
RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
private String name;
private int index;
private Color2(String name, int index) {
this.name = name;
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public String getInfo() {
switch (this) {
case RED: return "red";
case GREEN: return "green";
case BLANK: return "blank";
case YELLO: return "yello";
}
return null;
}
}
//第二种方式:实现了接口的枚举类
public enum Color2 implements Info {
RED("红色", 1){
@Override
public String getInfo() {
return "red";
}
},
GREEN("绿色", 2){
@Override
public String getInfo() {
return "green";
}
},
BLANK("白色", 3){
@Override
public String getInfo() {
return "blank";
}
},
YELLO("黄色", 4){
@Override
public String getInfo() {
return "yello";
}
};
private String name;
private int index;
private Color2(String name, int index) {
this.name = name;
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
@Test
public void testColorInterface() {
String info = Color2.YELLO.getInfo();
//yello
System.err.println(info);
}