[Enumeration] _ enumeration notes

Source: Internet
Author: User

[Enumeration] _ enumeration notes

Objectives of this chapter:
Measure the test taker's knowledge about the functions of enumeration.
You can use the enum keyword to define an enumeration class.

Enumeration introduction:
Before jdk1.5, Java can define a new type in two ways: class and interface. For most object-oriented programming, these two methods seem sufficient. However, in some special cases, these methods are not suitable. For example, to define a color class, it can only have three values: Red, green, and blue. If any other value is invalid, jdk1.5 can construct such code before, however, a lot of work may cause various insecure problems. After jdk1.5, the enumeration type (Enum) can be introduced to avoid these problems.

Class color {public static final color red = new color ("red"); public static final color green = new color ("green "); public static final color blue = new color ("blue"); Private string name; private color (string name) {This. name = Name;} public void setname (string name) {This. name = Name;} Public String getname () {return this. name;} public static color getinstance (int I) {Switch (I) {Case 1: {return red;} Case 2: {return green;} Case 3: {return blue ;}default: {return NULL ;}}}; public class colordemo01 {public static void main (string ARGs []) {color C1 = color. red; // obtain the red system. out. println (c1.getname (); color C2 = color. getinstance (3); system. out. println (c2.getname ());}}

At this point, the program limits the range of the objects that can be retrieved, so it reaches the enumeration function. The above is an enumeration method. In the earliest Java development, the concept of enumeration exists, so sometimes it is represented by an interface.

Interface color {public static final int Red = 1; // defines the red public static final int Green = 2; // defines the green public static final int Blue = 3; // defines the blue}

Because all the above values are directly represented by numbers, there are also problems during the operation.

Interface color {public static final int Red = 1; // defines the red public static final int Green = 2; // defines the green public static final int Blue = 3; // define blue} public class colordemo02 {public static void main (string ARGs []) {system. out. println (color. red + color. green); // Add colors }}

This operation is not very clear, so if you want to implement the enumeration operation before jdk1.5, it will be more troublesome.

Define an enumeration type

After jdk1.5, A New Keyword ---- Enum is introduced. You can directly define the enumeration type in the following format:
[Public] Enum Enumeration type name {
Enumeration object 1, enumeration object 2,..., enumeration object N;
}
Use the enum keyword to define.

Public Enum color {red, green, blue; // defines the enumerated types}

There are three values in enumeration.
In the future, only the three contents can be retrieved.

public class GetEnumContent{    public static void main(String args[]){        Color c = Color.BLUE;        System.out.println(c);    }}

Because the enumerated values have already specified a range, you can use foreach to output all the values and use "enumeration. Values" to obtain all the enumerated values.

public class PrintEnum{    public static void main(String args[]){        for(Color c:Color.values()){            System.out.println(c);        }    }}

You can also directly use the content on the switch statement.

Public class switchprintenum {public static void main (string ARGs []) {for (color C: color. values () {print (c) ;}} public static void print (color) {Switch (color) {Case RED: {system. out. println ("red"); break;} case Green: {system. out. println ("green"); break;} case Blue: {system. out. println ("blue"); break;} default: {system. out. println ("unknown color"); break ;}}}}

Common error: Case RED :{
Check whether the value exists in color. java.

Summary:
1. You can use enumeration to limit the value range.
2. You can use the enum keyword to define enumeration.

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.