Java enumeration applications

Source: Internet
Author: User

Java enumeration applications
Why enumeration?

For example, to define the day of the week, some people use 1-7, and some use 0-6, which can easily lead to problems.

Enumeration is to make a variable of a type have only one of several values. Otherwise, the compiler reports an error. enumeration allows the compiler to detect the invalid value entered by the source program during compilation, this goal cannot be achieved by simulating enumeration with common variables.

Use a common class to simulate the enumeration function:

1. Private the constructor
2. Each element has a public static member variable.

3. There may be several public or abstract methods, and the nextDay defined by the abstract method converts a large number of if... else into an independent class.

class WeekDay {private WeekDay(){}public final static WeekDay SUN = new WeekDay();public final static WeekDay MON = new WeekDay();public WeekDay nextDay(){return (this==SUN)?MON:SUN;}public String toString(){return (this==MON)?"MON":"SUN";}}public class EnumDemo {public static void main(String[] args) {System.out.println(WeekDay.SUN.nextDay());}}

The method can also be defined as abstract. The subclass of each fixed WeekDay object is deduplicated.
After compiling each enumeration class and test call class in a class, you can define the enumeration class as the internal class of the call class.

Public abstract class WeekDay {private WeekDay () {} public final static WeekDay SUN = new WeekDay () {public WeekDay nextDay () {return MON ;};}; public final static WeekDay MON = new WeekDay () {// anonymous internal class, subclass to write nextDay method public WeekDay nextDay () {return SUN ;};}; public abstract WeekDay nextDay (); public String toString () {return (this = MON )? "MON": "SUN ";}}

By the way, review:


Class Demo {}

New Demo () {}-> new a Demo subclass. The code in {} is the subclass code. This indicates that the subclass calls the construction method without parameters of the parent class.
New Demo (20) {}> new subclass instance object, call the construction method with parameters of the parent class


Basic Application of enumeration:

Define a Week Enumeration

Enumeration is a special class. Each element of this class is an instance object of this class. For example, you can use WeekDay. SUN. getClass (). getName ();


Public class EnumDemo {public static void main (String [] args) {System. out. println (WeekDay. SUN. nextDay (); Week w = Week. SUN; System. out. println (w); System. out. println (w. name (); System. out. println (w. ordinal (); // specifies the System number. out. println (w. getClass (); System. out. println (Week. valueOf ("SUN"); // converts the string to the corresponding enumeration element System. out. println (Week. values (). length); // Add each element in the enumeration to the array} public enum Week {// defines an enumeration class SUN, MON ;}}

Implement enumeration with Constructor

Public enum Week {// defines an enumeration class SUN, MON; // all things must be placed under the enumerated Value List private Week () {System. out. println ("WU") ;}; // The constructor of the enumeration class must be privateprivate Week (int I) {System. out. println ("I ");};}

According to the preceding main execution, all Week constructor without parameters are executed. As long as Week enumeration class is used, all static member variables in it will be initialized.

If you have to use the constructor with parameters, you must specify the parameters for the enumerated values.

public enum Week{SUN(5),MON();private Week(){System.out.println("WU");};private Week(int i){System.out.println("I");};}


Enumeration with abstract methods:

Public class EnumDemo {public static void main (String [] args) {System. out. println (Month. march. nextMonth ();} public enum Month {// January, February, March, 1_l; January (31) {public Month nextMonth () {return February ;}}, february (28) {public Month nextMonth () {return March ;}}, March (30) {public Month nextMonth () {return limit l ;}, limit L (31) {public Month nextMonth () {return January ;}}; public abstract Month nextMonth (); // abstract method. The return type is private int time of its own type; private Month (int time) {this. time = time;} public String toString () {if (this = January) return "January"; else if (this = February) return "February "; else if (this = March) return "March"; else return "April ";}}}

When enumeration has only one member, it can be implemented as a singleton.


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.