Java Enumeration Summary

Source: Internet
Author: User

  1. How do I define an enumeration class?

    1 // 4 levels defined 2 enum level{3    a,b,c,d4 }

  2. The essence of the enumeration class:

    1 classlevel{2      Public Static FinalLevel A =NewLevel ();3      Public Static FinalLevel B =NewLevel ();4      Public Static FinalLevel C =NewLevel ();5      Public Static FinalLevel D =NewLevel ();6}

  3. Enumeration classes can define constructor methods, properties, methods

    1 enumlevel{2A ("85~100", "excellent"),3B ("75~84", "Liang"),4C ("60~74", "Medium"),5D ("Less than 60", "poor");6     PrivateString score;7     PrivateString CType;8     PrivateLevel (String score,string CType) {//the construction method must be private9          This. score =score;Ten          This. CType =CType; One     } A      PublicString Getscore () { -         return  This. Score; -     } the      PublicString Getctpye () { -         return  This. CType; -     } - } +  Public classenumation { -      Public Static voidMain (string[] args) { + System.out.println (Level.a.getscore ()); A System.out.println (Level.a.getctpye ()); atlevel[] ls =level.values (); -     } - } - //Output Results - //85~100 - //Excellent

  4. Enum class with abstract method
    1 enumlevel{2A ("85~100"){3 @Override4          PublicString Getctpye () {5             returnExcellent;6         }7     },8B ("75~84") {9 @OverrideTen          PublicString Getctpye () { One             returnGood; A         } -     }, -C ("60~74") { the @Override -          PublicString Getctpye () { -             returnIn; -         } +     }, -D ("Under 60") { + @Override A          PublicString Getctpye () { at             returnPoor; -         } -     }; -     PrivateString score; -     PrivateLevel (String score) { -          This. score =score; in     } -      PublicString Getscore () { to         return  This. Score; +     } -      Public AbstractString Getctpye (); Each object has to override the abstract method the  *}

  5. Common methods

    1  Public classenumation {2      Public Static voidMain (string[] args) {3System.out.println (Level.c.name ());//returns the name of the enumeration constant4System.out.println (Level.c.ordinal ());//returns the ordinal of an enumeration constant (subscript starting from 0)5level[] ls = level.values ();//Traversing enumeration Types6          for(Level l:ls) {7 System.out.println (l);8         }9String letter = "B" ; Ten         //returns an enumeration constant of the specified enumeration type with the specified name, if letter is a non-enumerated type throws an exception Java.lang.IllegalArgumentException:No enum constant OneLevel B =level.valueof (letter); A System.out.println (b.b); -     } -}
      

    Output:
    C
    2
    A
    B
    C
    D
    B

Java Enumeration Summary

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.