献给我老公 - Java枚举类型
枚举类型是一种特殊的数据类型。具有枚举类型的变量,其取值范围会被限定在预定义的常量池中。该变量的值必须为预定义取值的其中之一。常见的例子有指南针的方向(东,西,南,北)和星期几。
由于是常量,枚举类型中定义的字段均为大写字母。
在Java编程语言中,通过关键字 enum 来定义一个枚举类型。例如,定义一个一周有几天的枚举类型:
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
在任何需要定义一个限定常量池的场景下,你就需要用到枚举类型。这包括自然界的枚举类型,例如银河系的星球,编译时就知道所有取值情况的数据集合——例如,一个菜单的选项,命令行标记等等。
这里给出一个以上定义的Day枚举类型应用实例:
public class EnumTest { Day day; public EnumTest(Day day) { this.day = day; } public void tellTheTruth() { switch (day) { case MONDAY: System.out.println("Mondays are bad if my husband is not around."); break; case FRIDAY: System.out.println("Fridays are better if my husband could have dinner with me."); break; case SATURDAY: case SUNDAY: System.out.println("Weekends are best if my husband is by my side."); break; default: System.out.println("Midweek days are good if my husband is able to have lunch with me."); break; } } public static void main(String[] args) { EnumTest firstDay = new EnumTest(Day.MONDAY); firstDay.tellTheTruth(); EnumTest thirdDay = new EnumTest(Day.WEDNESDAY); thirdDay.tellTheTruth(); EnumTest fifthDay = new EnumTest(Day.FRIDAY); fifthDay.tellTheTruth(); EnumTest sixthDay = new EnumTest(Day.SATURDAY); sixthDay.tellTheTruth(); EnumTest seventhDay = new EnumTest(Day.SUNDAY); seventhDay.tellTheTruth(); } }
输出为:
Mondays are bad if my husband is not around.
Midweek days are good if my husband is able to have lunch with me.
Fridays are better if my husband could have dinner with me.
Weekends are best if my husband is by my side.
Weekends are best if my husband is by my side.
Java编程语言中的枚举类型比其他语言中的同种类型更为强大。enum在声明时定义了一个类(称为一个枚举类型)。枚举类可以包含方法以及其他属性。在创建一个枚举时编译器会自动添加一些特殊方法。例如,它们都有一个静态的 values 方法,这个方法会按定义时的顺序返回枚举中的所有值。这个方法常与 for-each 连用以遍历一个枚举类型中的所有值。例如,下面这段选自 Planet 类的代码遍历了太阳系的所有星球。
for (Planet p : Planet.values()) { System.out.printf("Your weight on %s is %f%n", p, p.surfaceWeight(mass)); }
java.lang.Enum
. 由于一个类只能有一个父类,Java语言不支持多继承,所以枚举不能继承其他类。在接下来的例子中,Planet 是一个表示太阳系中所有星球的枚举类型。它们在定义时加入了体积和半径常量作为其属性。
每个枚举常量在声明时都有体积和半径作参数。在常量创建时这些值将被传入构造方法。Java要求先定义常量,优先于其他字段或者方法。并且当有其他字段和方法存在时,枚举常量列表必须以分号结尾。
除了属性和构造方法,Planet还提供了获取星球上表面重力和物体质量的方法。这里给出一个样例程序,以地球上的质量(任意单位)为入参,计算其在其他星球上的质量(相同单位):
public enum Planet { MERCURY (3.303e+23, 2.4397e6), VENUS (4.869e+24, 6.0518e6), EARTH (5.976e+24, 6.37814e6), MARS (6.421e+23, 3.3972e6), JUPITER (1.9e+27, 7.1492e7), SATURN (5.688e+26, 6.0268e7), URANUS (8.686e+25, 2.5559e7), NEPTUNE (1.024e+26, 2.4746e7); private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } private double mass() { return mass; } private double radius() { return radius; } // universal gravitational constant (m3 kg-1 s-2) public static final double G = 6.67300E-11; double surfaceGravity() { return G * mass / (radius * radius); } double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); } public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage: java Planet <earth_weight>"); System.exit(-1); } double earthWeight = Double.parseDouble(args[0]); double mass = earthWeight/EARTH.surfaceGravity(); for (Planet p : Planet.values()) System.out.printf("Your weight on %s is %f%n", p, p.surfaceWeight(mass)); } }
如果从命令行运行Planet.class并且输入参数175,会得到以下输出:
$ java Planet 175 Your weight on MERCURY is 66.107583 Your weight on VENUS is 158.374842 Your weight on EARTH is 175.000000 Your weight on MARS is 66.279007 Your weight on JUPITER is 442.847567 Your weight on SATURN is 186.552719 Your weight on URANUS is 158.397260 Your weight on NEPTUNE is 199.207413