Java programming: attaches methods to enumeration constants in enumeration enum

Source: Internet
Author: User

Java programming: attaches methods to enumeration constants in enumeration enum

Package enumeration; // append the method to the enumerated constant public class UnitsConverter {private static double numberToConvert = 0; public static void main (String [] args) {// TODO Auto-generated method stub if (args. length = 0) {System. out. println ("Usage: java UnitsConverter
 
  
"); System. exit (0);} numberToConvert = Double. parseDouble (args [0]); System. out. println ("lbs" + args [0] + "equals: \ n"); // traverses all elements enumerated by Converter for (Converter conv: Converter. values () {System. out. printf ("% s: % f % n", conv, conv. required mconversion (numberToConvert) ;}}// enumerative Converter: This method is required for every constant in the Converter // Converter. Enum Converter {// converts LBS into kilograms. Multiply the specified value by the constant 0.45359237 KG ("KG") {double weight mconversion (double f) {return f * = 0.45359237 ;}}, // converts LBS into CARAT ("carat") {double semi mconversion (double f) {return f * = 2267.96185 ;}}, // GMS ("gms") {double second mconversion (double f) {return f * = 453.59237 ;},// OUNCE ("ounce") {double second mconversion (double f) {return f * = 16 ;}}, // STONE ("stone") {double reverse mconversion (double f) {return f * = 0.071428571429 ;}}; private final String symbol; // constructor Converter (String symbol) {this. symbol = symbol ;}@ Override public String toString () {return symbol ;}// the defined abstract method abstrmconversion: Perform the conversion abstract double transform mconversion (double f );}
 

lbs 5.0 equals:KG:2.267962carat:11339.809250gms:2267.961850ounce:80.000000stone:0.357143


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.