Java enum Class (enum)

Source: Internet
Author: User

The enumeration classes are new after Java 5, can be used to encapsulate constants, and can also provide some methods for the use of constants.

Defines the syntax for an enumeration class:

   Public   enum    enumname{       //must be placed on the first row of      member 1 (A, B ... ), member 2 (A, B ... ), member 3 (A, b ...). )........; //a,b can not          Private   Type  A;        Private     Type  B;          ......        Private   enumname (Type  a,type  b ...)              {       a=A;                    b=b;                    .....             }           about A, b ..... The Get and set methods        }

Get an enumeration member in an enumeration class:

Enumerates the class names. Enumeration members;

By: Enumerates the class names. VALUES () can get all the enumeration members in the enumeration class;

enum   aa{     a,b,c;}

To traverse all the elements of the output, then:

 for (AA  s:aa.values ()  ) System.out.println (s);

The result is:

A

B

C

Write a Wheher.java class with the complete code as follows:

 enumAA {YES (2), NO (4); Private intvalue; PrivateAA (intI//You can also omit private{Value=i; }     Public intGetValue () {returnvalue; }     Public voidSetValue (intvalue) {         This. Value =value; }    } Public classwheher{ Public Static voidMain (String args[]) {System.out.println ("Output the value of the member No in the enumeration class----" +aa.no); System.out.println ("\ n---Call function isyes ()--"); System.out.println (Isyes (AA.         YES)); System.out.println (Isyes (aa.no)+ "\ n"); System.out.println ("View values replaced by enumeration constants---" +AA.                  Yes.getvalue ()); //set the value of an enumeration constantAa. Yes.setvalue (25); System.out.println ("\ n Change the value of Yes to----" +AA.               Yes.getvalue ()); }     Public  Static BooleanIsyes (AA a) {if(AA. Yes.equals (a))return true; Else return false; }}

The result of the operation is:

For an enumeration class, refer to: http://www.cnblogs.com/sister/p/4700702.html

Java enum Class (enum)

Related Article

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.