Java Learning Note 10--Enumeration

Source: Internet
Author: User
Tags comparable

Java Learning Note 10--Enumeration

Before JDK1.5, Java could have two ways of defining new types: Classes and interfaces. For most object-oriented programming, these two methods seem to be sufficient, but in some special cases, these methods are not appropriate. For example, to define a color class, which can only have red, Green, blue three values, and any other value is illegal, then JDK1.5 can construct such code before, but to do a lot of work, but also can bring a variety of unsafe problems. The enumeration type (enum) introduced after JDK1.5 can avoid these problems.

The so-called enumeration is the specified range of values, and all the content can only be obtained from the specified range.

Use simple classes to complete fixed-value problems with colors.

[Java]View Plaincopy
  1. Package com.itmyhome;
  2. Class color{
  3. public static final Color colorred = new Color ("red");
  4. public static final Color Colorgreen = new Color ("blue");
  5. public static final Color colorblue = new Color ("green");
  6. private String name;
  7. Public Color (String name) {
  8. this.name = name;
  9. }
  10. Public String GetName () {
  11. return name;
  12. }
  13. public void SetName (String name) {
  14. this.name = name;
  15. }
  16. }
  17. Public class T {
  18. public static void Main (string[] args) throws exception{
  19. Color cRed = color.colorred;
  20. System.out.println (Cred.getname ());
  21. }
  22. }


At this point, the program limits the scope of the object can be taken, so the function of enumeration is reached, the above is a way of enumeration, in the earliest Java development because there is no enumeration of this concept, it is sometimes used to express the interface.

[Java]View Plaincopy
    1. INTERFACE COLOR{  
    2.     public static  final int red = 1;   
    3.      Public static final int  Green = 2;  
    4.      public static final  int blue = 3;  
    5. }  


Defining an enumeration type

After JDK1.5, a new enum of the keyword type is introduced, and the enumeration type can be defined directly, in the following format:

"Public" enum enum type name {

Enumerate object 1, Enumerate Object 2, ..... Enumeration object N;

}

Use the enum keyword definition.

[Java]View Plaincopy
    1. Public enum Color {
    2. Red,green,blue;
    3. }

Because the enumeration already has a range specified, you can use foreach for all of the output, and use the enumeration. VALUES () Form to get all the enumerated content.

[Java]View Plaincopy
  1. Package com.itmyhome;
  2. Enum Color {
  3. Red,green,blue;
  4. }
  5. Public class T {
  6. public static void Main (string[] args) throws exception{
  7. For (Color c:color.values ()) {
  8. System.out.println (c);
  9. }
  10. }
  11. }


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

[Java]View Plaincopy
  1. Package com.itmyhome;
  2. Enum Color {
  3. RED, GREEN, BLUE;
  4. }
  5. Public class T {
  6. public static void Main (string[] args) throws exception{
  7. For (Color c:color.values ()) {
  8. print (c);
  9. }
  10. }
  11. public static void print (color color) {
  12. switch (color) {
  13. Case red:{
  14. System.out.println ("Red");
  15. Break ;
  16. }
  17. Case green:{
  18. System.out.println ("green");
  19. Break ;
  20. }
  21. Case blue:{
  22. System.out.println ("Blue");
  23. Break ;
  24. }
  25. default:{
  26. System.out.println ("Unknown Color");
  27. }
  28. }
  29. }
  30. }


Enum

It is clear from the previous that you can define an enumeration using the Enum keyword, which actually represents the java.lang.Enum type, that is, an enumeration type declared with an enum is equivalent to defining a class, and this class inherits the Java.lang.Enum class by default.

The Java.lang.Enum class is defined as follows:

[Java]View Plaincopy
    1. Public abstract class Enum<e extends enum<e>>
    2. Extends Object
    3. Implements Comparable<e>, Serializable

This class defines the use of generic mechanisms, and implements the comparable interface as well as the serializable interface, proving that this type is comparable and can be serialized.

Main methods for enumerating classes

How to construct an enum class:

[Java]View Plaincopy
    1. Protected Enum (String name, int ordinal)


The construction method receives two parameters, a name that represents the enumeration, and a sequence number that represents the enumeration.

[Java]View Plaincopy
    1. For (Color c:color.values ()) {
    2. System.out.println (C.name () +"----" +c.ordinal ());
    3. }


If you want to use some text to represent the color information, you can define the attribute and its own construction method in the enumeration in the form of the earliest color class, but once you have defined the parameter construct, you must explicitly call the constructor method when declaring the enumeration object, and pass the parameters.

[Java]View Plaincopy
  1. Package com.itmyhome;
  2. Enum Color {
  3. Red ("red"), Green ("green"), Blue ("blue"); //Incoming parameters
  4. private String name;
  5. Private Color (String name) { //define constructor method
  6. this.name = name;
  7. }
  8. Public String GetName () {
  9. return name;
  10. }
  11. public void SetName (String name) {
  12. this.name = name;
  13. }
  14. }
  15. Public class T {
  16. public static void Main (string[] args) throws exception{
  17. For (Color c:color.values ()) {
  18. System.out.println (c.ordinal () +"----" +c.name () +"----" +c.getname ());
  19. }
  20. }
  21. }

Java.util.EnumSet and Java.util.EnumMap are two enumeration collections. Enumset guarantees that the elements in the collection are not duplicated; The key in Enummap is the enum type, and value can be any type

Java Learning Note 10--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.