Enum class to create the principles of [fine]

Enum class to create the principles of [fine]

I. Introduction

Enumeration class generally define a set of applications that require constant, which is generally used as described in the service business scenarios different states.

Second, the enumeration of class definitions

1, the role of the enumeration class

Obtaining a constant number of fixed set of constants.

2, under what circumstances enumerated classes

Defining a set of constants, and the constants are fixed number of recommended enumeration class obtain constant.

3, the advantage of using the acquired enumeration class constant

* Enumeration class constructor is private, so other classes can not create class object to ensure that the number of class object constant is fixed.

* Type Security: Call enum class can only pass a defined object name, passing in a no-defined name will be thrown.

 

Third, create an enumeration type

The enumeration class is java5.0 version adds features not previously enumerated classes.

There are two ways to create enumeration class, the first class evolved into a custom enumeration type enum enumeration official deduced the principles of the official java enum enumeration class.

  • Custom enumeration class
//自定义枚举类

public class EnumTest {
    //1、申明 EnumTest 对象的属性
    private final String name;

    //2、私有化类的构造器
    private EnumTest(String name) {
        this.name = name;
    }

    //3、创建枚举类的对象
    public static final EnumTest SPRING = new EnumTest("春天");
    public static final EnumTest SUMMER = new EnumTest("夏天");
    public static final EnumTest AUTUMN = new EnumTest("秋天");
    public static final EnumTest WINTER = new EnumTest("冬天");

    //4、获取枚举类对象的属性
    public String getName() {
        return name;
    }

    //5、提供toString
    @Override
    public String toString() {
        return "EnumTest{" +
                "name='" + name + '\'' +
                '}';
    }
}
  •  enum enum class

This summary provided by the official enumeration class enumeration enum class instances created by observing and following custom enum class differences:

Creating enumeration class principle enum keyword:
     * 1. Enumeration enum class to create objects that duplicate the code has been simplified, leaving only the object name. Simplify the following effects.
     * Simplified front: public static Final EnumTest SPRING = new new EnumTest ( "Spring");
     * simplified to: SPRING ( "Spring")
     *
     * 2 among the plurality of objects divided by commas, and finally ending with a semicolon.
     * SPRING ( "Spring"),
     * SUMMER ( "Summer"),
     * AUTUMN ( "Autumn"),
     * WINTER ( "Winter");

     *
     * 3. The default enum class inheritance java.lang.Enum, rather than inherit the Object class.
     * For example: the public enum EnumPass compiled class is a class EnumPass the extends the Enum Final public
     *
     * 4. the java.lang.Enum override default toString method, and therefore may not be written in the enumeration class toString method. Custom
     * enum class inheritance Objec, so it is necessary to rewrite toString method.

public enum EnumPass {
    /**
     * enum 关键字创建枚举类原理:
     * 1.使用enum 枚举类创建对象将重复的代码进行了简化,简化效果如下。
     * 简化前:public static final EnumTest SPRING = new EnumTest("春天");
     * 简化后:SPRING("春天"),
     *
     * 2.多个对象之间用逗号 分割,最后用 分号结束
     * 
     * 3.枚举类默认继承java.lang.Enum,而不是继承Object类。
     *  例如:public enum EnumPass 类 编译后是这样的 public final class EnumPass extends Enum
     *
     * 4. java.lang.Enum默认重写了toString方法,因此在枚举类中可以不写toString方法。自定义的
     * 枚举类继承Objec,所以需要重写toString方法。
     */
    SPRING("春天"),
    SUMMER("夏天"),
    AUTUMN("秋天"),
    WINTER("冬天");

    //2、申明 EnumTest 对象的属性
    private final String name;

    //3、提供有参构造器
    EnumPass(String name) {
        this.name = name;
    }

    //4、获取枚举类对象的属性

    public String getName() {
        return name;
    }
}

Fourth, call the enumeration class

public class App {
    public static void main(String[] args) {

        //第一种方式,自定义枚举类
        EnumTest spring = EnumTest.SPRING;
        System.out.println("自定义枚举类:"+spring);

        //第二种方式,enum关键类创建枚举类
        EnumPass spring1 = EnumPass.SPRING;
        System.out.println("enum创建枚举类:" +spring1.getName());
    }
}

Test Results

自定义枚举类:EnumTest{name='春天'}
enum创建枚举类:春天

 

Published 316 original articles · won praise 117 · views 420 000 +

Guess you like

Origin blog.csdn.net/m0_38039437/article/details/104892003