Android Usage Enumeration pros and cons and alternatives

Source: Internet
Author: User

An enumeration should not be used on Android, which should be used instead of @xxxdef annotations.

Disadvantages of using Enum

Each enumeration value is an object, which increases the additional memory consumption when used, so the enumeration consumes more memory than the Integer and String.

More use of enums increases the size of the DEX file, resulting in more overhead for the runtime, which requires more space for our application.

If your app uses many enums, it's a good idea to replace them with an integer or String, but there's still a problem.

Since all this is said, what better way to solve it?

1  Public classsextest{2 3      Public Static enumSex {4 Man , Women5     }6 7     Privatesex sex;8     //Set Gender9      Public voidsetsex (Sex Sex) {Ten          This. Sex =sex; One     } A  -     //Get Sex -      PublicString Getsex () { the         if(Sex.man = = Sex)return "male"; -         if(Sex.women = = Sex)return "female"; -         return "Unknown"; -     } +  -      Public Static voidMain (string[] args) { +         //the entry parameter must be one of the enumeration constants in the sex enumeration class A         //Never allow input no more sex enum the constants defined inside at Setsex (Sex.man); -String Resultsex =getsex (); -System. out. println ("Resultsex:"+resultsex); -         //output: resultsex: Male -     } -}
Solution Solutions

Since the type of the parameter is too wide to be unsafe, I just have to limit the parameter to a certain type of collection, and I'm done.

is a drop, the @intdef/@StringDef + @interface to limit the parameters.

First you need to add a dependency in the Build.gradle file

dependencies {compile ' com.android.support:support-annotations:24.2.  0'}

Then use the code as follows:

 Public classMainactivity extends Activity { Public StaticFinalintMALE =0; Public StaticFinalintFEMALE =1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (r.layout.main_activity); Person Person=NewPerson ();    Person.setsex (MALE); (Button) Findviewbyid (r.id.test)). SetText (Person.getsexdes ());classPerson {@SEXPrivate intsex;  Public voidSetsex (@SEXintsex) {         This. Sex =sex; } @SEX Public intGetsex () {returnsex; }     PublicString getsexdes () {if(Sex = =MALE) {            return "male"; } Else {            return "female"; }}} @IntDef ({MALE, FEMALE}) @Retention (Retentionpolicy.source) Public@interface SEX {}}

If we try to pass a value that is not within the limit when we call the Setsex () method, then the compilation will not pass and there is an error message. Similarly, we can also use @stringdef.

As we can see here, @SEX annotations can be placed on attribute definitions, parameters, return values, and so on to restrict data types. If we assign a Int parameter directly setSex() , the IDE will give you an immediate error:

The use of Android enumeration pros and cons and alternatives

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.