Java-enumeration: java Enumeration type usage

Source: Internet
Author: User

Java-enumeration: java Enumeration type usage

The enumerated type is a special data type that allows a variable to become a constant set. The variable must be equal to one of the predefined constant sets.

Enumeration type definition

The definitions of enumeration types are similar to those of classes. You can use the enum keyword declaration to have methods and fields. When you create an enumeration class, the compiler automatically adds some special methods to the enumeration class, for example, the values method can return an array consisting of all values in the enumeration. The order is the same as the order they define.

All enumeration classes are implicitly inherited from the java. lang. Enum class. Because classes in java can only inherit one parent class, the enumeration type cannot inherit other classes.

Note: The visible range modifier of the enumeration constructor must be private or not (visible in the package). constants in the enumeration are automatically created, and enumeration constructor cannot be called by itself.

The following is a definition of planetary information enumeration in the Solar System:

// Note that the keyword is enum instead of classpublic enum Planet {// eight constants are defined here. The last constant is followed by a semicolon MERCURY (3.303e + 23, 2.4127e6 ), VENUS (4.869e + 24, 6.0518e6), EARTH (5.976e + 24, 6.20.14e6), MARS (6.421e + 23, 3.20.2e6), 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; // The unit is kilometers private final double radius; // The unit is meters Planet (double mass, do Uble radius) {// constructor 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 voi D main (String [] args) {// This is the main method. When this program is run, the input gravity on the earth is used as a parameter to output the if (args) of other planets. length! = 1) {// The parameter passed into the main method must be a string and the format is as follows: System. err. println ("Usage: java Planet <earth_weight>"); System. exit (-1);} double earthWeight = Double. parseDouble (args [0]); double mass = earthWeight/EARTH. surfaceGravity (); // you can use EARTH or Planet directly. EARTH for (Planet p: Planet. values () System. out. printf ("Your weight on % s is % f % n", p, p. surfaceWeight (mass ));}}

 

The above code is output

$ 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.