Enum class constructors
The enum class should typically include specific enumeration objects, and it is convenient to add some descriptions to those enumerated objects. This allows you to use the constructor of the enumeration to describe the specific characteristics of the enumeration object. Normally the constructor is private, which is also normal, because the enumeration class itself cannot be modified. You can use ToString () to override the original method to make a custom description of the enumeration object.
Code Demo
Package date0818.demo1;/** * Enum type can have constructors * @author ZZW922CN * */public enum Education {//Note, enumeration type is separated by commas primary ("5-12"), Mi Ddle ("13-15"), Senior ("16-18"), College ("19-22");//internal field private String epoch;//constructor is private and does not allow external creation of enumeration objects private education (String Epoch) {This.epoch=epoch;} Gets the Epoch field public String Getepoch () {return epoch;} The ToString method that overrides object @overridepublic String ToString () {return super.tostring () + "(\" "+epoch+" \ ")";} Main method public static void main (string[] args) {//Note the values () method is not the method that the enum comes from, but the//java.lang.annotation enum that the enum object compiler comes with ElementType contains the values () method for (Education edu:Education.values ()) {System.out.println (EDU);}}}
Test results
Primary ("5-12") Middle ("13-15") Senior ("16-18") College ("19-22")
Java enumeration Type deep learning (ii)