Use of the "Java Programming Advanced-1" Enum enumeration

Source: Internet
Author: User

Enumerations are primarily used for enumeration constants, and here is a simple application. For example, a company has the following sections:
    • Research department:
    • Sales Department:
    • Finance Department:
(other departments temporarily do not enumerate) some information of the department is relatively fixed, at this time can consider using enumeration to illustrate;Enum classDepts.java package myenum; public enum depts {      finacedept("Finance Department","responsible for Corporate finance"), developdept("Research and Development department","responsible for software development work"), saledept("Sales Department","responsible for the sales work of the Company");     Private final String name_cn; //Chinese name Private final String description; //Description depts (String n,string desc) {This . NAME_CN = n;This . description = desc;    }    Public String getname_cn () { return name_cn;    }Public String getdescription () { return description;    }  }Test class: Enumtest.java package myenum; public class enumtest { Public static void main (String args[]) {For (depts dept:depts.values()) {System. Out. println (dept + "-" + DEPT.GETNAME_CN () + "-" + dept.getdescription ());        }System. Out. println (depts.developdept. getdescription ()); System. Out. println (depts.developdept);     }}Test results:
We can also participate in abstract methods for enumerations, and then let each enumeration value implement this abstract method. package myenum; public enum depts {      finacedept("Finance Department", "responsible for Corporate finance") { void printinfo () {System. Out. println ("Recent corporate financial information is good");         }    }, developdept("Development Department","responsible for software development work") { void printinfo () {System. Out. println ("the company uses Java as a research and development tool");         }    }, saledept("sales Department","responsible for the sales work of the Company") { void printinfo () {System. Out. println ("Company Sales target: 10000 units/year");         }    };     Private final String name_cn; //Chinese name Private final String description; //Description depts (String n,string desc) {This . NAME_CN = n;This . description = desc;    }    Public String getname_cn () { return name_cn;    }Public String getdescription () { return description;    }     abstract void printinfo ();}Test class: package myenum; public class enumtest { Public static void main (String args[]) {For (depts dept:depts.values()) {System. Out. Print (dept); dept.printinfo ();        }depts.developdept. Printinfo ();    }}Test results:




From for notes (Wiz)

Use of the "Java Programming Advanced-1" Enum enumeration

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.