使用List <string>类型的变量定义枚举

时间:2019-04-25 08:03:43

标签: enums

我正在开发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;
    }

2 个答案:

答案 0 :(得分:2)

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

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

应该是

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

答案 1 :(得分: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();

    }

}