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