31st: Replace ordinal with instance field

Source: Internet
Author: User

Enumerates the natural domains of a single int value association, based on the enumeration constants in the type of the numeric position, starting from 0 count. All enumerations have a ordinal method that returns the numeric position of each enumerated constant in the type.

Most programs do not require this method and are designed to be used for enumeration-based, generic data structures like Enumset and Enummap.

Instead of exporting the value associated with it based on the ordinal of an enumeration, save it in an instance field.

 Public enum Ensemble {    SOLO, DUET, TRIO, Quartet, quintet,    Sextet, Septet     , OCTET, Nonet, Dectet;  Public int Numberofmusicians () {        return ordinal () + 1;    }}

This enumeration represents a different number of musicians. If the enumeration constants are reordered, or if you add an enumeration constant with the same number of musicians as before, there is no way to implement it.

Modified to:

 Public enum Ensemble {    SOLO (1), DUET (2). TRIO (3), quartet (4),    ...;         Private Final int Numberofmusicians;    Ensemble (int  n) {        = n;    }}

Use the instance domain Numberofmusicians to save the number of musicians.

31st: Replace ordinal with instance field

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.