枚举是用于将变量分配给一组预定义常量的特殊数据类型。枚举主要用于将特定范围的值分配给所述变量时。
例如,当声明彩虹的颜色时,我们通常被限制在代表各种颜色的7个常量,我们可以用枚举来定义这些颜色,并有一种方法来表示这些常量。枚举是单个这意味着对于类加载器当处理需要固定单选选项或for Switch语句的代码时,此功能使枚举成为一个不错的选择。
我们不会像常规类那样用新关键字初始化枚举,但这并不能阻止它的强大功能:添加实例变量和方法的能力、接口的实现、拥有构造函数和使用抽象类的能力。枚举不能扩展其他类,它与类的主要区别在于它是静态的,它的声明是最终的。下面是一个基本枚举声明的示例:
class Enum{
//Defining the enum inside the class
public enum RainbowColours { RED, YELLOW, GREEN, BLUE, INDIGO, VIOLETS}
//Main method
public static void main(String[] args) {
//Reading the enum
for (RainbowColours c : RainbowColours.values())
System.out.println(c);
}}
使用枚举
java开发人员可以用几种方法轻松地应用Enums:
定义类内外的枚举
枚举可以在java中的类内部和外部运行。通过使用enum关键字定义枚举类型:
类内部的枚举:
class EnumA{
enum Season { WINTER, SPRING, SUMMER, FALL; } //semicolon(;) is optional here
public static void main(String[] args) {
Season s=Season.WINTER;//enum type is required to access WINTER
System.out.println(s);
}}
This returns an output of "WINTER"
类外部的枚举:
enum Directions{ NORTH, SOUTH, EAST, WEST}
class EnumB{
public static void main(String[] args) {
Directions d=Directions.WEST;
System.out.println(d);
}}
This returns an output "WEST"
在枚举中运行main方法
enum Season {
WINTER, SPRING, SUMMER, FALL;
public static void main(String[] args) {
Season ss=Season.SPRING;
System.out.println(ss);
}
}
This returns an output "SPRING"
对枚举使用switch语句
class EnumC{
enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
public static void main(String args[]){
Day day=Day.TUESDAY; //Sets enum day to TUESDAY
switch(day){
case SUNDAY:
System.out.println("Today is sunday");
break;
case MONDAY:
System.out.println("Today is monday");
break;
case TUESDAY:
System.out.println("Today is tuesday");
break;
default:
System.out.println("other day");
}
}}
This returns an output "Today is tuesday"
指定枚举常量的属性
public enum Enum //enum classes cant be initialized with key word "new" but can only be referenced since they are classloaded
{
GENDER("male","female"), CAT("lion","lioness"), BIRD("cock","hen");
private final String Masculine; //must be set to private final because each variable has already been pre-set above in the enum declaration
private final String Feminine;
Enum(String masculine, String feminine){ //no public keyword because the contructor isn't public and sets the value properties of the enum constants.
// This constructor sets the Properties of each enum constant
this.Masculine=masculine;
this.Feminine=feminine;
}
public String getMasculinity(){
return Masculine;
}
public String getFemininity(){
return Feminine;
} //decide to use camel casing for a clearer description
}
//Running the enum above
public class EnumTest {
public static void main(String[] args){
System.out.println("ennum values");
for (EnumClass class : EnumClass.values()){ //Traversing enum
System.out.println("For class "+ class+ " the masculine form is "+ class.getMasculinity() + " and the feminine name is "+class.getFemininity());
}
}
}
This returns Multiple outputs starting with the first loop case "For class GENDER the masculine form is male and the feminine form is female"
枚举类型的构造函数必须是包私有或私有访问。它自动创建在枚举体开头定义的常量。您不能自己调用枚举构造函数(因为枚举被设置为final)。
显示一组枚举常量
Enumset是用于枚举类型的集合的特殊实现之一。它是一个特殊的集合,用于使用诸如“range”、“of”、“copyOf”、“clone”等方法的枚举类。下面是一个enum实现的示例,它显示GENDER-BIRD中的枚举范围。
import java.util.EnumSet;
public enum Enum
{GENDER, CAT, BIRD;}
public class EnumTest {
public static void main(String[] args){
System.out.println(EnumSet.range(EnumClass.GENDER,EnumClass.BIRD));
}
}
Java编程语言枚举类型比其他语言中的枚举类型功能强大得多,Java开发人员可以使用它以简化的方式执行任务。