zoukankan      html  css  js  c++  java
  • 创建带值枚举

    自定义枚举1:

     1 package com.echo.springboot.constants;
     2 
     3 public enum AppConstants {
     4 
     5     COUNT_EVERYPAGE(8),
     6     CODE_SUCCESS(1),
     7     CODE_FAIL(0);
     8 
     9     private final int value;
    10 
    11     private AppConstants(int value){
    12         this.value=value;
    13     }
    14     public int getValue(){
    15         return value;
    16     }
    17 }

    注意构造函数是必须的并且是私有的,还有其中的value是必须的。上面是自定义的。

    自定义枚举2:

     1 package com;
     2  
     3 public enum Color {
     4      
     5      RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
     6      
     7      
     8     private String name ;
     9     private int index ;
    10      
    11     private Color( String name , int index ){
    12         this.name = name ;
    13         this.index = index ;
    14     }
    15      
    16     public String getName() {
    17         return name;
    18     }
    19     public void setName(String name) {
    20         this.name = name;
    21     }
    22     public int getIndex() {
    23         return index;
    24     }
    25     public void setIndex(int index) {
    26         this.index = index;
    27     }
    28      
    29  
    30 }
     1 package com;
     2  
     3 public class B {
     4  
     5     public static void main(String[] args) {
     6  
     7         //输出某一枚举的值
     8         System.out.println( Color.RED.getName() );
     9         System.out.println( Color.RED.getIndex() );
    10  
    11         //遍历所有的枚举
    12         for( Color color : Color.values()){
    13             System.out.println( color + "  name: " + color.getName() + "  index: " + color.getIndex() );
    14         }
    15     }
    16  
    17 }
    历经苦难而不厌,此乃阿修罗之道。
  • 相关阅读:
    java对redis的基本操作
    关于Java异常和错误的几个问题
    「hadoop」fs.defaultFS 9000 端口在外面连不上
    「ubuntu」修改权限和owner命令
    「ubuntu」pkexec超级修改权限
    「ubuntu」vim 基本使用
    「ubuntu」修改主机名
    「hadoop」ssh
    「vmware」虚拟机与主机共享目录
    「spring」定时任务(纯注解方式)
  • 原文地址:https://www.cnblogs.com/echo1314/p/10312679.html
Copyright © 2011-2022 走看看