Enumeration type (enum) description

Source: Internet
Author: User
Tags define abstract
Enum Type

1, overview, JDK1.5 added enum type, you can use an enum to define eg:

           public enum gender{
              Male,female;
           }
Each of these enumeration elements is an instance of the enumerated type, using and traversing
           Gender Gender = Gender.male;    Use for    
           (Gender s:gender.values ()) {//Traverse
                 System.out.println (s);  
           }
2. The difference between the enum class and the enum keyword
1. The enumeration type defined using the Enum keyword is actually equivalent to defining a class that inherits the Java.lang.Enum class
2. Each enumeration value is an instance of an enumerated type, which is preset as public static final
The Enum class has only one protected construction method
PROTECTD Enum (String name,int ordinal)
Ordinal: Enumerating the number of elements
In fact, once each enumerated element is declared, it is called automatically, and all numbers are numbered automatically (numbering starts with 0);
Using the valueof () method, you can find a specified object in an enumeration by enumerating the classes
           public enum color{
              red,blue;
           }
3, get the enumeration type Object two ways:
The first way:
       String s = "RED";
           Color red = color.valueof (s);
The second way:
       Color red = color.red;
The first way is to get the object by the value of a string, as long as we change the value of the string to arbitrarily change the object to be obtained.
The second way is to write dead code, no matter how the execution is to get the red object in the enumeration class, always the case.
4. Attributes and methods of enumerated types
Enumeration types can have properties and methods
The rules are as follows:
properties and methods must be defined after the element list declaration
public enum color{
              Red,blue;
              private String name;
              Public String GetName () {return
              this.name
              }}
           
5, defining the constructor method in the enumeration type
1. The enumeration's construction method can only be defined after the element list
2. The method of constructing an enumeration type can only be Private, do not write the default is also private
3. Element if you need to invoke a parameter constructor, then add the following element (parameters)
Explanation: The color type has only three fixed objects, red GREEN BLUE respectively, and the three object invokes both the parameter and the parameterless constructor at the time of creation
eg
      public enum Color {red
             ("Scarlet"), Green ("green"), BLUE;
             private String name;
             Public String GetName () {return
                this.name;
                 }
             A parameter builder
             Color (String name) {
                this.name = name;
             }
             Non-parameter constructor
             private Color () {
             }
              }
Main
Get one of the three fixed objects in color
Color C = color.red;
Get the value of the Name property in the Red object
This value is printed "red."
Because it was the red object that was created.
Passed to the property name by the constructor of a parameter.
Declaring the list of elements in an enumeration type is actually
In creating a fixed object of this enumeration type
String name = C.getname ();
Enumerations implicitly inherit from an enum therefore, no longer can inherit other classes
6, enumeration implementation interface
Enumerations can implement interfaces in the following two ways
1. Like the normal class class, implement an interface in an enumeration class
2. An abstract method in an enumeration that implements the abstract method in an interface individually or in each enumeration element object, respectively, to implement this interface
        public interface sofa{
            void sit ();
          } 
The first way to implement the interface
    Public enum Color implements sofa{
             red ("Red"), Green ("green"), BLUE;
             private String name;
             Public String GetName () {return
                this.name;
                 }
             A parameter builder
             Color (String name) {
                this.name = name;
             }
             The parameterless constructor is
             implemented individually in private Color () {
             }
             //enum, and each enumerated element object can be invoked to this method public
             void Sit () {
                //...
             }
        }
The second way to implement the interface
  Public enum Color implements sofa{
             //The abstract method of implementing this interface individually in each enumerated element object in the future, each enumerated element object is called by its own independent implementation of this sit method
             red ("red") { Public
                void Sit () {
                    //...}}
             , green
             ("green") {public
                void sit () {
                    //...}}
             ,            
             blue{public
                void Sit () {
                    //...
                    }
             };
             private String name;
             Public String GetName () {return
                this.name;
                 }
             A parameter builder
             Color (String name) {
                this.name = name;
             }
             Non-parameter constructor
             private Color () {
             }
              }
7. Defining abstract methods in enumerations
Multiple abstract methods can be defined in an enumeration, but each element in the enumeration must implement these abstract methods, as follows:
The public enum Color {
             red{public
            String getInfo () {//red implements the abstract method return
               "Red";
            }
             },
             green{
            The public string GetInfo () {//green implements the abstract method return
               "green";
            }
             ,
             blue{public
            string GetInfo () {// Implement abstract method in Blue return
               "Blue";
            }
             ;
             Public abstract String GetInfo ();//define abstract method
          }

The above is an introduction to enumerations

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.