How to use Java enum

Source: Internet
Author: User

Just get started, the notes are clear.

Writing enumeration Classes

/*** You can use an interface or class to wrap an enumeration element so that it can call the portal uniformly*/ Public InterfaceTESTENUMINTFC {/*** Create an enumeration object*/     Public enumTestenum {//1. General-Definition enumeration Entries//Enabled, Disabled//2. Generic Definition Enumeration Entries//Enabled (1), Disabled (0);//private int value;//testenum (int value) {//this.value = value;//    }        //3. Complex-defined enumeration entries//(The parameters provided by the constructor method of the enumeration item parameter correspond)Enabled (1, "Enabled"), Disabled (0, "Disable"); //(store values using private variables)        Private intvalue; PrivateString text; //The constructor method can only be private, and is constructed to assign a valueTestenum (intvalue, String text) {             This. Value =value;  This. Text =text; }        //You can define a method for external invocation to get the value of each property (providing a method for an enumeration item)//TestEnum.Enabled.toInt ()         Public intToInt () {return  This. Value; }         PublicString Totext () {return  This. Text; }        //you can define a static method to get text based on value (provides methods for an enumeration class)//Testenum.gettext (1)         Public StaticString GetText (intvalue) {             for(Testenum item:TestEnum.values ()) {if(Value = =item.value) {returnItem.text; }            }            return NULL; }        //you can override the ToString () method to implement custom output@Override PublicString toString () {return Super. toString (); }    }}

Write Call Main ()

 Public Static voidMain (string[] arge) {//Call the same ToString () method, output: EnabledSystem.out.println (TestEnumIntfc.TestEnum.Enabled); //gets the text, output: enabled, based on the selected enumeration entrySystem.out.println (TestEnumIntfc.TestEnum.Enabled.toText ()); //gets the value, based on the selected enumeration entry, output: 1System.out.println (TestEnumIntfc.TestEnum.Enabled.toInt ()); //converts to a string based on the selected enumeration key, output: EnabledSystem.out.println (TestEnumIntfc.TestEnum.Enabled.toString ()); //returns the text of the value corresponding to the selected enumeration class and the value passed in, output: EnabledSystem.out.println (TestEnumIntfc.TestEnum.getText (1)); //gets an array of all the items in the enumeration class and loops         for(Testenumintfc.testenum e:testenumintfc.testenum.values ()) {System.out.println (E.totext ()); }    }

How to use Java enum

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.