The use of enumerations under Android, and the confusion with int conversion

Source: Internet
Author: User

Enumerated constants have been used in C + + environments, but when used under Android, it is quite different from the C + +.
Under Android, enumerations are actually classes.
The use of sensory difficulties is mainly the conversion between enumerations and Int. If the enumeration is defined as shown in weekday, it can also be converted through the ordinal () and the values () [] method.
Unfortunately, I used the enumeration type shown in the following weekday_2, and I did not find the corresponding conversion method.
Finally, there is no way to modify the enumeration to a generic constant definition, as follows:

1  Public Static Final  Short Max_buffer_size = 2048;  
1  Public classTest {2      Public enumWeekday3     {  4 Sun,5 mou,6 Tue,7 Thu,8 Fri,9 Sat,Ten Wed, One     };  A        -      Public enumweekday_2 -     {   theSun (0x1),   -MOU (0x2),   -Tue (0x3),   -Thu (0x4),   +Fri (0x5),   -Sat (0x6),   +Wed (0x7),   ALarge (0x99);//If this value is increased, what is the value obtained by the ordinal () method?  at        -         Private intISet;  -Weekday_2 (intivalue) {   -              This. ISet =Ivalue;  -         }   -        in          Public intGet () { -             return  This. ISet;  to         }   +     };  -}

For the above two enumerations, define weekday and weekday_2:

1 int x = weekday.sun.ordinal ();   2 Weekday sun = Weekday.values () [x];  

And

1 int x = weekday_2.sun.ordinal ();   2 weekday_2 Sun = weekday_2.values () [x];  

What's the difference? Are the values of the other members the same except for the large members in weekday_2?

1 voidTestenumvalue () {2Tstenum.weekday Day =TstEnum.weekday.sun; 3Tstenum.weekday_2 day2 =TstEnum.weekday_2.sun; 4Tstenum.weekday_2 Day2_large =TstEnum.weekday_2.large; 5       6     intIday =day.ordinal (); 7     intIDay2 =day2.ordinal (); 8     intIday2large =day2_large.ordinal (); 9   Ten    OneSystem.out.println ("Enum Test" + "Value of Sun:" + integer.tostring (iday) + "/" +integer.tostring (iDay2) A+ ". Large: "+integer.tostring (Iday2large));  -}

The result of the output after execution is: Enum testvalue of sun:0/0. Large:7
Weekday_2 's member Sun did not output its true value, but instead output the subscript.
corresponding, weekday.values () [x]; is also the subscript. If you know that a variable value of weekday_2 is: integer 0x99, you want to turn it into the corresponding enumeration. If you use Weekday.values () [0x99], you will be directly out of bounds.


(1) Execution: Javac Test.java
Compile without errors.


(2) Execution: JAVAP Test
Get the following output:

1 Compiled from ' Test.java '  2publicclass  Test {  3   int  x;   4   Test$weekday Sun;   5    Public Test ();   6 }  

(3) Execution: JAVAP test$weekday
Get the following output:

1Compiled from "Test.java"2  Public Final classTest$weekdayextendsJava.lang.enum<test$weekday> {  3    Public Static FinalTest$weekday Sun; 4    Public Static FinalTest$weekday mou; 5    Public Static FinalTest$weekday Tue; 6    Public Static FinalTest$weekday Thu; 7    Public Static FinalTest$weekday Fri; 8    Public Static FinalTest$weekday Sat; 9    Public Static FinalTest$weekday Wed; Ten    Public Statictest$weekday[] VALUES ();  One    Public Statictest$weekday valueOf (java.lang.String);  A   Static {};  -}

(4) Execution: JAVAP test$weekday_2
Get the following output:

1Compiled from "Test.java"2  Public Final classTest$weekday_2extendsJava.lang.enum<test$weekday_2> {  3    Public Static FinalTest$weekday_2 Sun; 4    Public Static Finaltest$weekday_2 mou; 5    Public Static Finaltest$weekday_2 Tue; 6    Public Static Finaltest$weekday_2 Thu; 7    Public Static Finaltest$weekday_2 Fri; 8    Public Static FinalTest$weekday_2 Sat; 9    Public Static Finaltest$weekday_2 Wed; Ten    Public Statictest$weekday_2[] VALUES ();  One    Public Statictest$weekday_2 valueOf (java.lang.String);  A    Public intGet ();  -   Static {};  -}

From the above-compiled code can draw the following conclusions:
(1) The weekday and Weekday_2 enumeration classes are final class, i.e. they cannot be inherited, and they are inherited from enum
(2) The enumeration value is a weekday or Weekday_2 object, and it is the static final decorated

The use of enumerations under Android, and the confusion with int conversion

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.