Improve the quality of your Java code: Use constructors to help describe enumeration items

Source: Internet
Author: User

I. Analysis in general, the enumeration item that we often use has only one attribute, that is, the sorting number. Its default value is from 0, 1, 2 ....... However, in addition to the sorting number, the enumeration has one or more attributes. II. For example, you can declare the business value by enumerative constructors, define the optional values, and add attributes. See the following code:

Enum Role {Admin ("Administrator", new Lifetime (), new Scope (); User ("common User", new Lifetime (), new Scope ()); // Chinese description private String name; // Role life cycle private Lifetime lifeTime; // permission range private Scope scope; Role (String _ name, Lifetime _ lt, Scope _ scope) {name = _ name; lifeTime = _ lifeTime; scope = _ scope;}/** name, lifeTime, and scope get methods are relatively simple and will not be repeated */}

 

You can read a book, so that developers can have a three-dimensional and Multidimensional Awareness of the Admin and User constants, including names, lifecycles, and scopes, you can also easily obtain such attributes in the program. 3. We recommend that you define the description for each enumeration in the enumeration definition, especially in large-scale project development, A large number of constant item definitions use enumeration item descriptions more user-friendly and concise than adding annotations to an interface constant or class constant.

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.