Java enumeration Type deep learning (ii)

Source: Internet
Author: User

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)

Related Article

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.