Enumeration class code implementation

Preface:

      Realize you need to know to use an enum class and enum class pay attention to:

  1. enum and a class, interface status is the same.
  2. Use enum defined default enumeration class inherits java.lang.Enum, rather than inherit the Object class. Enumeration class may implement one or more interfaces.
  3. Enumerate all instances of the class must be on the first line display, without the use of the new keyword, without explicitly call the constructor. Automatically add public static final modification.
  4. Use enum definition, non-abstract class uses an enumeration final modified default, can not be inherited.
  5. Enum class constructor can only be private.         

Enum class implementation:

//枚举类的实现
public enum DataEnum {
      
      /**
       * 线上01, 这是枚举类的实例,必须放在第一行展示,多个实例之间使用逗号隔开,实例的修饰符默认使用public static final
       */
      ONLINE("01"),
      
      /**
       * 线下02
       */
      OFFLINE("02");
      
      
      private String value;
      
      //枚举类的构造器必须私有构造器
      private DataEnum(String value){
            this.value = value;
      }
    
      //公共的get方法
      public String getValue() {
            return value;
      }
    
      //公共的set方法
      public void setValue(String value) {
            this.value = value;
      }
}



//枚举类简单的使用
public class EnumTest {
      
      public static void main(String[] args) {
            System.out.println(DataEnum.ONLINE.getValue());
            DataEnum.ONLINE.setValue("03");//重新设置枚举值
            System.out.println(DataEnum.ONLINE.getValue());
      }
}

 

Published 20 original articles · won praise 31 · views 9577

Guess you like

Origin blog.csdn.net/feichitianxia/article/details/93380541