From http://www.cnblogs.com/ggjucheng/archive/2012/12/03/2800427.html
English from http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
Enumeration type. It is the type of a field containing a fixed number of constants. For common examples, the direction of the compass is east-west, north-south, and day of the week.
Because they are constants, the names of all enumerated fields are generally uppercase letters.
Java programming language, declare an enumeration type using the enum keyword. For example, if you want to specify the day of the whole week, the enumerated type is:
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
The enumeration type should be used whenever a fixed group constant is needed. This includes natural enumeration types, such as the planet of the Milky Way, which you can know at compilation. For example, menu selection and command line flag.
The following code describes how to use the Day Enumeration type declaration:
public class EnumTest { Day day; public EnumTest(Day day) { this.day = day; } public void tellItLikeItIs() { switch (day) { case MONDAY: System.out.println("Mondays are bad."); break; case FRIDAY: System.out.println("Fridays are better."); break; case SATURDAY: case SUNDAY: System.out.println("Weekends are best."); break; default: System.out.println("Midweek days are so-so."); break; } } public static void main(String[] args) { EnumTest firstDay = new EnumTest(Day.MONDAY); firstDay.tellItLikeItIs(); EnumTest thirdDay = new EnumTest(Day.WEDNESDAY); thirdDay.tellItLikeItIs(); EnumTest fifthDay = new EnumTest(Day.FRIDAY); fifthDay.tellItLikeItIs(); EnumTest sixthDay = new EnumTest(Day.SATURDAY); sixthDay.tellItLikeItIs(); EnumTest seventhDay = new EnumTest(Day.SUNDAY); seventhDay.tellItLikeItIs(); }}
The output is:
Mondays are bad.Midweek days are so-so.Fridays are better.Weekends are best.Weekends are best.
Java programming language Enumeration type is more powerful than other programming languages. The enum Declaration defines the class (called the enum type ). Enumeration class body, which can contain methods and other fields. The compiler automatically adds special methods for enum. For example, a static values method returns an enum value array in the declared order. This method usually combines the for-each structure to traverse all values of the enum type. For example, the followingPlanet
The code in the class demonstrates traversing all the planets in the galaxy.
for (Planet p : Planet.values()) { System.out.printf("Your weight on %s is %f%n", p, p.surfaceWeight(mass));}
Note: All enum classes are implicitly inherited.java.lang.Enum
. Because java does not support multi-inheritance, all enum cannot inherit other classes.
In the following example,Planet is an enumeration type that represents all the planets in the galaxy. They are defined with constant quality and RADIUS attributes.
Each enumerated constant has a quality and radius parameter declaration. These values are passed in during constant initialization through constructor. Java requires constants to be defined first, followed by fields and methods. Therefore, before fields and methods, The enum Constant list must end with a semicolon.
Note: The enum type construction method must be private or private access in the package. It automatically creates a constant that starts to create a declaration in the enum body, and cannot directly call the enum constructor.
For its properties and construction methods, there are methods on the planet that allow you to retrieve the gravity and weight of your surface on each planet. The following is an example program that calculates based on your weight on the Earth (Any unit) and prints your weight on all the planets (in the same unit ):
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)); }}
If you runPlanet. class, the parameter is 175, and the output is:
$ java Planet 175Your weight on MERCURY is 66.107583Your weight on VENUS is 158.374842Your weight on EARTH is 175.000000Your weight on MARS is 66.279007Your weight on JUPITER is 442.847567Your weight on SATURN is 186.552719Your weight on URANUS is 158.397260Your weight on NEPTUNE is 199.207413