类和对象

枚举类型(Enum Type)

枚举类型是一种特殊的数据类型,使一个变量是一组预定义的常量。变量必须等于已预先定义的值之一。常见的例子包括罗盘方向( NORTH, SOUTH, EAST 和 WEST)和星期几。

使用关键字 enum ,下面是一个星期几的枚举列子:

  1. publicenumDay{
  2. SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
  3. THURSDAY, FRIDAY, SATURDAY
  4. }

使用枚举类型,需要一组固定的常数。这包括自然枚举类型,如在我们的太阳系的行星,菜单上的选项,命令行标志,等等。

下面是一些代码,展示如何使用上面定义的 Day 枚举:

  1. classEnumTest{
  2. Day day;
  3. publicEnumTest(Day day){
  4. this.day = day;
  5. }
  6. publicvoid tellItLikeItIs(){
  7. switch(day){
  8. case MONDAY:
  9. System.out.println("Mondays are bad.");
  10. break;
  11. case FRIDAY:
  12. System.out.println("Fridays are better.");
  13. break;
  14. case SATURDAY:case SUNDAY:
  15. System.out.println("Weekends are best.");
  16. break;
  17. default:
  18. System.out.println("Midweek days are so-so.");
  19. break;
  20. }
  21. }
  22. publicstaticvoid main(String[] args){
  23. EnumTest firstDay =newEnumTest(Day.MONDAY);
  24. firstDay.tellItLikeItIs();
  25. EnumTest thirdDay =newEnumTest(Day.WEDNESDAY);
  26. thirdDay.tellItLikeItIs();
  27. EnumTest fifthDay =newEnumTest(Day.FRIDAY);
  28. fifthDay.tellItLikeItIs();
  29. EnumTest sixthDay =newEnumTest(Day.SATURDAY);
  30. sixthDay.tellItLikeItIs();
  31. EnumTest seventhDay =newEnumTest(Day.SUNDAY);
  32. seventhDay.tellItLikeItIs();
  33. }
  34. }

输出为:

  1. Mondays are bad.
  2. Midweek days are so-so.
  3. Fridays are better.
  4. Weekends are best.
  5. Weekends are best.

下面是一个 Planet 示例,展示了 枚举值的 for-each 遍历:

  1. publicenumPlanet{
  2. MERCURY (3.303e+23,2.4397e6),
  3. VENUS (4.869e+24,6.0518e6),
  4. EARTH (5.976e+24,6.37814e6),
  5. MARS (6.421e+23,3.3972e6),
  6. JUPITER (1.9e+27,7.1492e7),
  7. SATURN (5.688e+26,6.0268e7),
  8. URANUS (8.686e+25,2.5559e7),
  9. NEPTUNE (1.024e+26,2.4746e7);
  10. privatefinaldouble mass;// in kilograms
  11. privatefinaldouble radius;// in meters
  12. Planet(double mass,double radius){
  13. this.mass = mass;
  14. this.radius = radius;
  15. }
  16. privatedouble mass(){return mass;}
  17. privatedouble radius(){return radius;}
  18. // universal gravitational constant (m3 kg-1 s-2)
  19. publicstaticfinaldouble G =6.67300E-11;
  20. double surfaceGravity(){
  21. return G * mass /(radius * radius);
  22. }
  23. double surfaceWeight(double otherMass){
  24. return otherMass * surfaceGravity();
  25. }
  26. publicstaticvoid main(String[] args){
  27. if(args.length !=1){
  28. System.err.println("Usage: java Planet <earth_weight>");
  29. System.exit(-1);
  30. }
  31. double earthWeight =Double.parseDouble(args[0]);
  32. double mass = earthWeight/EARTH.surfaceGravity();
  33. for(Planet p :Planet.values())
  34. System.out.printf("Your weight on %s is %f%n",
  35. p, p.surfaceWeight(mass));
  36. }
  37. }

在命令行,输入参数为 175 时,
输出如下:

  1. $ java Planet175
  2. Your weight on MERCURY is66.107583
  3. Your weight on VENUS is158.374842
  4. Your weight on EARTH is175.000000
  5. Your weight on MARS is66.279007
  6. Your weight on JUPITER is442.847567
  7. Your weight on SATURN is186.552719
  8. Your weight on URANUS is158.397260
  9. Your weight on NEPTUNE is199.207413