Use the enumerated ordinal () method with caution ~

Source: Internet
Author: User

Copyright Disclaimer: I have been touched by a section on enumeration in the Java impact tive book ~ Not original ~

Let's start with an enumeration:

 
Public Enum enumarmylvltype {defaults 0, en_armu_lvl_type_divine,/* 1. x/en_armu_lvl_type_outstanding,/* 2. excellent */en_armu_lvl_type_elite,/* 3. elite */Category 4, Category 5, en_armu_lvl_type_normal,/* 6. normal */;

 

 

This is a simple enumeration type that represents the monster's quality level.

 

Sometimes, we need to pass data out. For example, if the android client sends data to the C ++ server, it is impossible for us to directly transfer the enumerated variables ?~ This is so cute ~

 

As a result, I am smart and will normally do this:

Int iarmylvltype = enumarmylvltype. en_armu_lvl_type_outstanding.ordinal ();

This is correct. It's good ~ However, this is not safe ~

Why? This is because the ordinal method is not specifically used for the corresponding relationship. It is only a sequence number of the enumerated variable.

The truth is simple, just like...

In short, this is not professional ~ Even so, it cannot be said to be wrong.

It's like... You know, I'm no better

We cannot be clever ~ What the ordinal () method gets is a sequence number, which is just a sequence number. We are not allowed to use it for correspondence ~

Hey, don't yell at me ~ Look at the following practice. Are you feeling a lot better ?~

This uses the constructor of the enumeration class to specify the value corresponding to the enumeration variable ~

 
Public Enum enumarmylvltype {/* Find anything different here? No "cmd," these variables are full. If (1) is added, the constructor */en_armu_lvl_type_divine (1),/* 1. god will */en_armu_lvl_type_outstanding (2),/* 2. excellent */en_armu_lvl_type_elite (3),/* 3. elite */en_armu_lvl_type_normal (6),/* 6. normal */; private int inum = 0;/* constructor. Remember that it must be private ~ */Private enumarmylvltype (INT inum) {This. inum = inum;}/* replace the ordinal () method with this method, which is safe, professional, and optional ~ */Public int tonumber () {return this. inum ;}}

 

 

How is it? See it? In this way, we can convert it like this:

 
Int iarmylvltype = enumarmylvltype. en_armu_lvl_type_outstanding.tonumer ();

In this way, the tonumber () function is specifically used for the corresponding relationship. This function is dedicated, professional, and has no side effects. Please try it ~

For knowledge about enumeration constructors, go to Baidu ~ You are not allowed to ask me ~

PS:

Some may say, why is it so troublesome? Why does en_armu_lvl_type_normal (6),/* 6. Common */correspond to 6 numbers? Simply change to 4, there is no trouble at all, so that you can easily use the ordinal method ~!

This is not the case if you listen to my explanation. You listen to my explanation... wife, please listen to my explanation. This is just a misunderstanding...

Sorry, it's so amazing.

 

In fact, this is the reason. This is for expansion and convenience of servers.

I don't know the specific reason. In short, the serverProgramThe clerk told me to reserve some space for convenient management.

 

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.