Enumeration of Java 2

Source: Internet
Author: User

First understand the nature of enumerations in Java.

Everything in the Java World is a class, and here's an example to explain the nature of the enum:

 Packagecn.xnchall.enumeration; Public classGrade { Public voidTest () {print (grade.    A); }             Public voidPrint (grade g) {//print A B C D E            }}/*class grade{Private Grade () {} public static final grade A = new Grade ();    public static final Grade B = new Grade ();    public static final Grade C = new Grade ();    public static final Grade D = new Grade (); public static final Grade E = new Grade ();}*/enumgrade{//class a,b,c,d,e; Object}

The grade class and the Grade enumeration type work the same way, which means that the enumeration type is a more rigorous class. It does not allow the new grade class, because the underlying implementation is private. To implement the new operation externally, it must be implemented inside the grade.

The following is a formal introduction enumeration!

1. Defining constructors, methods, and fields for enumerations

 Packagecn.xnchall.enumeration; Public classDome { Public Static voidTest () {print (grade.    A); }             Public Static voidPrint (grade g) {//print A B C D ESystem.out.println (G.getvalue ()); }         Public Static voidMain (string[] args) {test (); }}/*class grade{Private Grade () {} public static final grade A = new Grade ();    public static final Grade B = new Grade ();    public static final Grade C = new Grade ();    public static final Grade D = new Grade (); public static final Grade E = new Grade ();}*/enumgrade{//class A 100-90 B 89-80 C 79-70 D 69-60 E 59-0A ("100-90"), B ("89-80"), C ("79-70"), D ("69-60"), E ("59-0")    ; PrivateString value;//the score for each field    PrivateGrade (String value) { This. Value =value; }         PublicString GetValue () {return  This. Value; }}

The result that will be executed above is: 100-90

Not to be continued

Will be added: enumerations with abstract methods

Enumeration of Java 2

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.