Enumeration and single-instance dark-horse programmer

Source: Internet
Author: User

I. The concept of enumeration for the concept of enumerations, we can first ask a question, for everything is the class of everything is the object of the Java language, enumeration is not a class? Enum can define a constructor, with normal methods, even abstract methods, except that it cannot inherit, so the enum can basically be seen as a regular class.      Now that you can think of it as a regular class, we might as well write a class-impersonation enumeration. The case scenario of this article: Take our corporate philosophy of Mison and the job responsibilities of each position as an example: Public classMisoner {PrivateMisoner () {}//Lecturer         Public Static FinalMisoner Technical_adviser =NewMisoner (); //Employment Guidance         Public Static FinalMisoner employment_guide =NewMisoner (); //Software Outsourcing         Public Static FinalMisoner software_development =NewMisoner (); //Consulting Center         Public Static FinalMisoner Consulting_center =NewMisoner ();} Copy code This class is different from other classes where the constructor is private and the object appears in constant form, why is it so defined? It is not difficult to think about the two points from which an enumeration cannot be instantiated and directly accesses elements through enumerations. Now let's take a look at how the above code is implemented by enumerations: Public enummisonerenum{//lecturer, Employment Guidance Center, Software Outsourcing Center, consulting centerTechnical_adviser, Employment_guide, Software_development, Consulting_center;} The code that the replication code implements through enumeration is much more concise. The next two questions are:1. What is the constructor of the enumeration called? 2How is the abstract method of the enumeration implemented? Let's look at question 1th first: Packagecomplex; Public classMisoner {PrivateMisoner (String position) {System.out.println ("Post:" +position); }         Public Static FinalMisoner Technical_adviser =NewMisoner ("technical director"));  Public Static FinalMisoner employment_guide =NewMisoner ("Employment Guide"));  Public Static FinalMisoner software_development =NewMisoner ("Project manager"));  Public Static FinalMisoner Consulting_center =NewMisoner ("consultant"); The code above the copy code is familiar, easy to understand, and how the enumeration implements the above code, as follows: Packagecomplex; Public enummisonerenum {technical_adviser ("Technical director"), Employment_guide ("Employment Guidance")), Software_development ("Project Manager"), Consulting_center ("consultant")); PrivateMisonerenum (String position) {System.out.println ("Post:" +position); The replication code is next to question 2nd, and again, we compare the enumeration classes and enumerations by impersonation: Packagecomplex; Public Abstract classMisoner {PrivateMisoner (String position) {System.out.println ("Post:" +position); }        //defining inner class objects         Public Static FinalMisoner Technical_adviser =NewMisoner ("technical director")) {                 Public voidWork () {System.out.println ("Instructors provide first-class Java technical training");        }        };  Public Static FinalMisoner employment_guide =NewMisoner ("Employment Guide")) {                 Public voidWork () {System.out.println ("Employment Guidance Center helps students achieve high-paying employment");        }        };  Public Static FinalMisoner software_development =NewMisoner ("Project manager")) {                 Public voidWork () {System.out.println ("Software Outsourcing center to provide students with commercial outsourcing paid actual combat");        }        };  Public Static FinalMisoner Consulting_center =NewMisoner ("consultant") {                 Public voidWork () {System.out.println ("Counseling Center provides students with all the services of life and study");        }        }; //Defining abstract methods         Public Abstract voidWork ();} The code to copy the code and use the enum is as follows: Packagecomplex; Public enummisonerenum {technical_adviser ("Technical Director") {                 Public voidWork () {System.out.println ("Instructors provide first-class Java technical training"); }}, Employment_guide ("Employment Guidance") {                 Public voidWork () {System.out.println ("Employment Guidance Center helps students achieve high-paying employment"); }}, Software_development ("Project Manager") {                 Public voidWork () {System.out.println ("Software Outsourcing center to provide students with commercial outsourcing paid actual combat"); }}, Consulting_center ("Consulting Consultant") {                 Public voidWork () {System.out.println ("Counseling Center provides students with all the services of life and study");                }        }; PrivateMisonerenum (String position) {System.out.println ("Post:" +position); }                 Public Abstract voidWork ();}

Enumeration and single-instance dark-horse programmer

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.