【问题标题】:Define enum with variable with List<String> type使用 List<String> 类型的变量定义枚举
【发布时间】:2019-04-25 18:02:09
【问题描述】:

我正在开发 Spring Boot 应用程序,我有一个案例,我想将枚举定义为 List 作为类型。但是我在传递列表时遇到语法错误。我们有解决此语法错误的解决方法吗?

我的代码

EMAIL("001", "email", "Send To Email"),
    SMS("002", "slack", "Send To SMS"),
    EMAIL_SMS("003", "email", "Send to SMS and Email");


    private String code;
    private String description;
    private List<String> dest = new ArrayList<>();

    NotificationCenterCodeEnum(String  code, List<String> dest, String description) {
        this.code = code;
        this.dest=dest;
        this.description = description;
    }

【问题讨论】:

    标签: enums


    【解决方案1】:

    您没有将第二个参数作为列表传递:

    EMAIL("Code-001", "email", "Send To Email"),
    

    应该是

    EMAIL("Code-001", Arrays.asList("email"), "Send To Email"),
    

    【讨论】:

      【解决方案2】:

      试试这个:

      enum Notification {
      
          EMAIL("code 1", "description 1", "email-2", "email-2"),
          SMS("code 2", "description 2", "num-1", "num-2", "num-3");
      
          Notification(String code, String description, String... dest) {
              this.code = code;
              this.description = description;
              this.dest = dest;
          }
      
          private String code;
          private String description;
          private String[] dest;
      
          // getters ...
      }
      

      使用:

      public class Hello {
      
          public static void main(String[] args) {
      
              String[] emails = Notification.EMAIL.getDest();
              String[] nums = Notification.SMS.getDest();
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2012-03-10
        • 1970-01-01
        • 1970-01-01
        • 2011-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多