Java programming: Enumerating enums, attaching methods to enumeration constants

Source: Internet
Author: User

Package enumeration;//Append method to enumeration 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<weight in Pounds>");        System.exit (0);        } Numbertoconvert = Double.parsedouble (Args[0]);        System.out.println ("lbs" +args[0]+ "equals:\n"); Iterates over all elements of the Converter enumeration for (Converter conv:Converter.values ()) {System.out.printf ("%s:%f%n", Conv,co        Nv.performconversion (Numbertoconvert)); }}}//Enumeration Converter: Each constant in the converter//converter needs to implement this method. Enum converter{//Put the pound into kilograms, you need to multiply the specified value by constant 0.45359237 kg ("kg") {double performconversion (double f) {Retur        n f*=0.45359237;        }},//Convert the pound to carat CARAT ("CARAT") {double performconversion (double f) {return f*=2267.96185; }},//GMS ("gms") {double PerfoRmconversion (double f) {return f*=453.59237;        }},///ounce ("ounce") {double performconversion (double f) {return f*=16;        }},//Stone ("stone") {double performconversion (double f) {return f*=0.071428571429;        }    };    private final String symbol;    Construction method Converter (String symbol) {this.symbol=symbol;    } @Override Public String toString () {return symbol; }//Defined abstract method Performconversion: Performs the conversion abstract double performconversion (double f);}

LBS 5.0 equals:kg:2.267962carat:11339.809250gms:2267.961850ounce:80.000000stone:0.357143


Java programming: Enumerating enums, attaching methods to enumeration constants

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.