[JAVA] java enumeration class, java Enumeration

Source: Internet
Author: User

[JAVA] java enumeration class, java Enumeration

I. Under what circumstances do I use enumeration classes?

Sometimes the objects of a class are limited and fixed. In this case, it is more convenient to use enumeration classes?

2. Why not use static constants to replace enumeration classes?

    public static final int SEASON_SPRING = 1;    public static final int SEASON_SUMMER = 2;    public static final int SEASON_FALL = 3;    public static final int SEASON_WINTER = 4;

Enumeration classes are more intuitive and type-safe. Using constants has the following defects:

1. the type is insecure. If the season parameter is required in a method, if the constant is used, the form parameter is of the int type. If the developer passes in any int type value, but if it is of the enumeration type, only objects contained in the enumeration class can be passed in.

2. There is no namespace. The developer must start with SEASON _ when naming, so that when another developer looks at the code, they will know that these four constants represent the SEASON respectively.

Iii. Enumeration

Let's first look at a simple enumeration class.

package enumcase;public enum SeasonEnum {    SPRING,SUMMER,FALL,WINTER;}

Iv. Enumeration

You can also define attributes and methods in the enumeration class, but they are static and non-static.

Package enumcase; public enum SeasonEnum {SPRING ("SPRING"), SUMMER ("SUMMER"), FALL ("Autumn"), WINTER ("WINTER"); private final String name; private SeasonEnum (String name) {this. name = name ;}public String getName () {return name ;}}

In fact, the constructor is called by default when you write the enumeration class instance in the first line. Therefore, you need to input parameters here because no parameter constructor is explicitly stated, only constructors with parameters can be called.

The constructor must be defined as private so that such objects cannot be declared elsewhere. Enumeration classes should generally be designed as immutable classes, and their Field should not be changed, which will be safer and the code will be more concise. So we use private final to modify the Field.

5. Enumeration class implementation Interface

The enumeration class can implement one or more interfaces. Like a common class, all methods defined in the interface need to be implemented when an interface is implemented. If not fully implemented, the enumeration class is abstract, but does not need to be explicitly added with abstract modification, added by default.

  

Package enumcase; public enum Operation {PLUS {@ Override public double eval (double x, double y) {return x + y ;}}, MINUS {@ Override public double eval (double x, double y) {return x-y ;}, TIMES {@ Override public double eval (double x, double y) {return x * y ;}, DIVIDE {@ Override public double eval (double x, double y) {return x/y ;};/*** abstract method, different implementations are provided by different enumerated values. * @ Param x * @ param y * @ return */public abstract double eval (double x, double y); public static void main (String [] args) {System. out. println (Operation. PLUS. eval (10, 2); System. out. println (Operation. MINUS. eval (10, 2); System. out. println (Operation. TIMES. eval (10, 2); System. out. println (Operation. DIVIDE. eval (10, 2 ));}}

Operatio classes are actually abstract and cannot be used to create enumeration values. Therefore, when declaring enumeration values, abstract methods are implemented. This is actually the implementation of anonymous internal classes, the curly braces are a class body. Let's take a look at the compiled files.

  

Five class files are generated in symbiosis, which proves that PLUS, MINUS, TIMES, and DIVIDE are anonymous internal class instances of Operation.

6. The expression in the switch statement can be an enumeration value.

Java 5 adds the enum keyword and the switch extension.

Package enumcase; public class SeasonTest {public void judge (SeasonEnum s) {switch (s) {case SPRING: System. out. println ("SPRING is a perfect choice. "); Break; case SUMMER: System. out. println (" You are going to swim in the SUMMER. "); Break; case FALL: System. out. println (" You must travel in the FALL. "); Break; case WINTER: System. out. println (" it would be nice if it was snowing in WINTER. "); Break ;}} public static void main (String [] args) {SeasonEnum s = SeasonEnum. SPRING; SeasonTest test = new SeasonTest (); test. judge (s );}}

The case expression directly writes enumeration values without the need to add enumeration classes as limits.

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.