Kago takes you to learn big data series from scratch Java Chapter 11: Enumeration

Key points of the course:

  • Basic definition of enumeration
  • Basic use of enumeration

11.1. Introduction to enumeration

11.1.1. The concept of enumeration

Enumeration is also a custom data type, which is a reference data type. Enumeration is often used to describe data with limited value range.

For example:
  • Gender: there are only two values, which can be represented by enumeration
  • Month: only 12 values, which can be represented by enumeration
  • Week: there are only seven values, which can be represented by enumeration

 

11.2. Basic definition and use of enumeration

11.2.1. definition of enumeration

To define enumeration type, the keyword enum is required. The name of the enumeration is an identifier, which follows the naming of the great hump.

/**
 * @Description   Definition of enumeration
 */
public enum Gender {
    // List all possible values of this enumeration object
    // The elements in the enumeration are also identifiers, which follow the naming method of the big hump
    Male, Female
}
public enum Month {
    Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
}
public enum Week {
    Mon, Tue, Wed, Thu, Fri, Sat, Sun
}

11.2.2. Use of enumeration

Enumeration is a custom data type that can declare variables. When using, directly use the form of enumeration type. Enumeration value to get enumeration value.

/**
 * @Description   Use of enumeration
 */
public class Test {
    public static void main(String[] args) {
        // Use of enumeration
        Gender gender1 = Gender.Male;
        Gender gender2 = Gender.Female;

        Month m1 = Month.Jan;
        Month m2 = Month.Nov;

        Week week1 = Week.Sat;
    }
}

11.3. Member definition in enumeration

11.3.1. Analysis of enumeration

Enumeration, in fact, can be considered as a final subclass of the Object class. Cannot be inherited by other classes or enumerations.

/**
 * @Description  Use of enumeration
 */
public class EnumTest {
    public static void main(String[] args) {
        // 1. Get an enumeration object
        Gender gender = Gender.Male;
        // 1.1. Proof method 1: enumerate objects and call methods in the Object class, indicating that these methods are inherited from the Object class.
        String str = gender.toString();
        // 1.2. Proof method 2: the Object type can be transformed upward.
        Object obj = gender;
    }
}
enum Gender {
    Male, Female
}

11.3.2. Attribute definition in enumeration

/**
 * @Description    Attribute definition of enumeration
 */
public enum Gender {
    Male, Female;
    // 1. To define attributes, methods and construction methods in enumeration, you need to write them below enumeration elements!
    //    If you need to define members in an enumeration, you need to add a semicolon after the last enumeration element.
    public String desc;
}

11.3.3. Definition of construction method in enumeration

/**
 * @Description   Enumeration construction method definition
 */
public class Test2 {
    // In order to prevent duplication with the Gender enumeration in the current package, the static internal enumeration is written here
    private static enum Gender {
        // In fact, the so-called enumeration element is actually a static object of the current class.
        Male("male"), Female("female");
        // Add attribute
        private String desc;
        // Add a constructor to assign a value to this property
        // Define construction methods in enumerations. Generally, they are only used in current enumerations
        // Therefore, the construction method of enumeration is generally private
        Gender(String desc) {
            this.desc = desc;
        }
    }
    public static void main(String[] args) {
        // 1. Get enumeration objects
        Gender gender = Gender.Male;
    }
}

11.3.4. Method definition in enumeration

/**
 * @Description   Method definition of enumeration
 */
public enum Gender {
    Male, Female;
    // 1. To define attributes, methods and construction methods in enumeration, you need to write them below enumeration elements!
    //    If you need to define members in an enumeration, you need to add a semicolon after the last enumeration element.
    public String desc;

    // 2. Definition method
    public void show() {
        System.out.println("Method definition in enumeration");
    }
    public static void display() {
        System.out.println("Definition of static methods in enumeration");
    }
}

11.3.5. Method rewriting in enumeration

/**
 * @Description   Method override
 */
private static enum Gender {
     // In fact, the so-called enumeration element is actually a static object of the current class.
     Male("male"), Female("female");
     // Add attribute
     private String desc;
     // Add a constructor to assign a value to this property
     // Define construction methods in enumerations. Generally, they are only used in current enumerations
     // Therefore, the construction method of enumeration is generally private
     Gender(String desc) {
         this.desc = desc;
     }

     @Override
     public String toString() {
         return this.desc;
     }
 }

11.3.6. Enumeration implementation interface

/**
 * @Description   Interface implementation
 */
enum Gender implements MyInterface {
    @Override
    public void test() {
        System.out.println("Methods in interfaces");
    }
}

interface MyInterface {
      void test();
}

11.3.7. Enumeration value

/**
 * @Description   enum 
 */
enum Gender implements MyInterface {
    // In fact, the so-called enumeration element is actually a static object of the current class.
    Male("male") {
        // Here, it is equivalent to an anonymous inner class
        // Here, you can override the methods in Gender
        @Override
        public void test() {
            System.out.println("Male Rewriting implementation of");
        }
    },Female("female");
    // Add attribute
    private String desc;
    // Add a constructor to assign a value to this property
    // Define construction methods in enumerations. Generally, they are only used in current enumerations
    // Therefore, the construction method of enumeration is generally private
    Gender(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return this.desc;
    }

    @Override
    public void test() {
        System.out.println("Methods in interfaces");
    }
}

Tags: Attribute

Posted on Thu, 18 Jun 2020 23:50:42 -0400 by jdsflash