J2SE 5.0 Instance---Enumeration

Source: Internet
Author: User
Tags abstract define arithmetic constant eval return
J2SE Enumeration
In the past, we had to use an integer constant instead of an enumeration, and with the release of J2SE 5.0, the method was finally gone.

A simple enumeration type is defined as follows:

public enum Weather

{

Sunny,rainy,cloudy

}

Enumerations can be used in switch statements:

Weather Weather=weather.cloudy;

Switch (weather)

{

Case SUNNY:

System.out.println ("It ' s Sunny");

Break

Case Cloudy:

System.out.println ("It ' s Cloudy");

Break

Case Rainy:

System.out.println ("It ' s Rainy");

Break

}

Enumeration types can have their own construction methods, but they must be private or have other methods defined, such as the following code:

public enum Weather {

SUNNY ("It is SUNNY"),

Rainy ("It is Rainy"),

Cloudy ("It is Cloudy");





Private String description;





Private Weather (String description) {

This.description=description;

}



Public String description () {

return this.description;

}

}

The following code is a use of this enumeration:

For (Weather w:weather.values ())

{

System.out.printf ("Description of%s is \"%s\ ". \ n", w,w.description ());

}





Weather Weather=weather.sunny;

System.out.println (weather.description () + "Today");

If we have an enumeration type that represents arithmetic and we want to define a method in which to do different operations for different values, then we can define this:

public enum Operation {

PLUS, Minus, times, DIVIDE;





Do arithmetic op represented by this constant

Double eval (double x, double y) {

Switch (this) {

Case Plus:return x + y;

Case Minus:return XY;

Case Times:return x * y;

Case Divide:return x/y;

}

throw new Assertionerror ("Unknown op:" + this);

}

}

The problem with this is that if you don't have the last line of the statement that throws the exception, the compilation cannot pass. And if we want to add a new operation, we must always remember to add the corresponding action in the eval, and throw an exception if we forget.

J2SE 5.0 provides a way to solve this problem by declaring the Eval function abstract and then writing a different implementation for each value, as follows:

public enum Operation {

PLUS {Double eval (double x, double y) {return x + y;}},

Minus {Double eval (double x, double y) {return xy}},

Times {Double eval (double x, double y) {return x * y;}},

DIVIDE {Double eval (double x, double y) {return x/y;}};





Abstract double eval (double x, double y);

}



This avoids the two problems mentioned above, but the amount of code increases, but as future improvements to the various Java development Ides, the problem of code volume should be diluted.


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.