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