Deep Learning of java enumeration applications and deep learning of java Enumeration

Source: Internet
Author: User

Deep Learning of java enumeration applications and deep learning of java Enumeration

I. Differences between enumeration and static Constants

When talking about enumeration, let's first think about how it is different from the constant modified by the public static final String.

Two advantages of enumeration are as follows:

1. Guaranteed type security: the caller cannot transmit an int or String equivalent at will;

2. The code is highly readable;

For example:

In actual programming, there are often such "datasets", their values are stable in the program, and the elements in the "dataset" are limited. For example, the four data elements of spring, summer, autumn, and winter constitute the "dataset" of the Four Seasons ".

You have written the get (String season) method. The input type can only be String type, and the String type can only be spring or summer. Autumn. Winter ).

At this time. You write four string constants

Public class Common {public static final String SPRING = "SPRING"; public static final String SEASON = "SUMMER"; public static final String SUMMER = "Autumn "; public static final String AUTUMN = "Winter ";}

Put get (Common. SEASON), it is true to put "Spring", but at this time you will find a hidden danger, you get (String season), after all, put the String type, if new colleagues or uninformed colleagues do not know this method, they can only put "spring, summer, autumn, and winter ", it has a character string such as get ("small"). At this time, it will not report an error during the compilation period. It will only find the error after running.

In order to prevent the above risks, enumeration has emerged.

Public enum Season {SPRING ("SPRING"), SUMMER ("SUMMER"), AUTUMN ("AUTUMN"), WINTER ("WINTER ");.....}

At this time, we modify the parameter passing through the get method and change it to get (Season season). At this time, we add get (Season. SPRING). This ensures that only these parameters can be passed in.

Ii. Understanding Enumeration

First of all, we must make it clear that enumeration is actually a class. I will write an enumeration to understand it.

// We regard enumeration as a common class public enum Season {SPRING (1, "SPRING"), SUMMER (2, "SUMMER"), AUTUMN (3, "AUTUMN "), WINTER (4, "WINTER"); // The last one must be a semicolon; otherwise, an error is reported./* We can understand it as * public static final Season SPRING = new Season (1, spring); * public static final Season SUMMER = new Season (2, SUMMER); * public static final Season AUTUMN = new Season (3, AUTUMN ); * public static final Season WINTER = new Season (4, WINTER); * since it is an object, we can understand it very well. * // ** 1. there are two parameters in the above object, so we must have this type of constructor * 2. this is private, because it cannot be a new object */private Season (int code, String name) {this. name = name; this. code = code;} // private String name of the object's property; private int code; // Method for retrieving object properties public String getName () {return this. name;} public String getCode () {return this. name;} // get the object through code, and we can get other attributes of the object public static Season decode (int code) {Season season = null; for (Season type: Season. values () {if (type. code = code) {season = type; break;} return season;} // re-toString method public String toString () {return this. name ;}}

In the above example, enumeration is well explained. It is no different from common classes, but several attribute objects are created in another way, this must also be written to the constructor with attributes.

Some enumeration features are listed here by the way:

1. It cannot have a public constructor. This ensures that the customer Code cannot create an enum instance.
2. Enumeration cannot inherit other classes, because it inherits java. lang. Enum by default.
3. The constant value address is unique and can be directly compared with = to improve the performance.
4. Enum also provides the values method, which allows you to easily traverse all enumeration values.
5. There is also an oridinal Method for Enum. This method returns the order of enumeration values in enumeration classes. This order depends on the order in which enumeration values are declared.

Iii. Common enumeration usage

First: switch Application

Create an enumeration first:

Public enum Common {INSERT, MODIFY, DELETE} // because there is no parameter object, you can use the default system constructor. You do not need to write attributes or methods.

Write the implementation code

Public class CommonUtils {public static void getType (Common common) {Common c = common; switch (c) {case INSERT: System. out. println ("insert operation"); break; case MODIFY: System. out. println ("modify"); break; case DELETE: System. out. println ("delete operation"); break;} public static void main (String [] args) {getType (Common. DELETE); // background output: DELETE operation }}

The second method is to obtain other values by using the key value

Enumeration class

Public enum Season {SPRING (1, "SPRING", "SPRING kite flying"), SUMMER (2, "SUMMER", "SUMMER swimming"), AUTUMN (3, "Autumn", "Autumn Outing"), WINTER (4, "WINTER", "WINTER pot"), private Season (int code, String name, String bz) {this. code = code; this. name = name; this. bz = bz;} private int code; private String name; private String bz; public static Season decode (int code) {Season season = null; for (Season type: Season. values () {if (type. code = code) {season = type; break;} return season;} public int getCode () {return code;} public String getName () {return name ;} public String getBz () {return bz ;}}

Test class

Well, I will write so much, and I will learn more in the future. Thank you for your support.

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.