Java enum type enum

Source: Internet
Author: User

An enumeration type, the Enum keyword, was added to Java SE5. Before this, when you need to create a set of shaped constants, but these enumerated values do not necessarily limit their own values within the range of the constant set, so it is not safe to do so, and it is not easy to use. Now that we have an enum, we have solved the above problem.

Although an enum looks like a new data type, the enum is actually a class and has its own method (ToString () ordinal () static values ()). We can use an enum as a way to create a data type when used.

An instance of an enumeration type is a constant, so it is represented in uppercase letters.

1. First create an enumeration type

1 Package Enumdemo;2 /**3 * Create enum type4 * @author DYF5  *6  */7  Public classEnum {8      Public enumtest{9 Not ,Ten MILD, One MEDIUM, A Hot , - Flaming; -     } the}

2. Test the Enum's own method (ToString () ordinal () static values ()),

1 Package Enumdemo;2 /**3 * Test Enum's Own method ToString () ordinal () static values ()4  */5 import enumDemo.Enum.test;6 7  Public classTestenum {8      Public Static voidMain (string[] args) {9         //-----------------------------------------------Ten         /** One * Use of enumeration classes A          */ -Test E =test. MEDIUM; -System. out. println (e); the         //----------------------------------------------- -          -         /** - * When creating an enum, the compiler will automatically add some useful features. As + * ToString method, showing the name of the enum instance - * ordinal () static values () method, which indicates the order of declarations for a particular enum constant +          */ A          for(Test t:test.values ()) { atSystem. out. println (T +"---ordinal---"+t.ordinal ()); -         } -          -         //----------------------------------------------- -          -     } in}

Printing results:

MEDIUM
Not---ordinal---0
MILD---ordinal---1
MEDIUM---ordinal---2
Hot---ordinal---3
Flaming---ordinal---4

3, enum and switch are used in combination, because switch is to be selected in a limited set of possible values, so the formal combination of an enum is perfect.

1 Package Enumdemo;2 /**3 * enum is used in combination with switch4  */5 import enumDemo.Enum.test;6 7  Public classTestenumandswitch {8 Test T;9      Publictestenumandswitch (Test t) {Ten          This. T =T; One     } A      -      Public voiddescribe () { -System. out. println ("This testenumandswitch is"); the         Switch(t) { -          CaseNot:system. out. println ("Not spicy @ All"); -              Break; -          CaseMild:system. out. println ("=========1"); +          CaseMedium:system. out. println ("a little hot"); -              Break; +          CaseHot:system. out. println ("=========2"); A          CaseFlaming:system. out. println ("=========3"); at         default: System. out. println ("maybe too hot"); -         } -     } -      -      Public Static voidMain (string[] args) { - Testenumandswitch inT1 =Newtestenumandswitch (test. not), -t2 =Newtestenumandswitch (test. MEDIUM), toT3 =Newtestenumandswitch (test. Hot); + t1.describe (); - t2.describe (); the t3.describe (); *     } $}

Printing results:

This testenumandswitch is
Not spicy @ All
This testenumandswitch is
A little Hot
This testenumandswitch is
=========2
=========3
Maybe too hot

Java enum type 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.