Java 使用 lombok 创建枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49837774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 03:08:36  来源:igfitidea点击:

Creating enums with lombok

javaenumslombok

提问by Priyank Thakkar

I am using project lombok with my application. I was creating an enum. If I use

我在我的应用程序中使用了 lombok 项目。我正在创建一个枚举。如果我使用

@AllArgsConstructor

@AllArgsConstructor

annotation with my enum, it doesn't recognise the constructor, enum throws and error that it cannot take string argument.

使用我的枚举进行注释,它无法识别构造函数、枚举抛出和不能接受字符串参数的错误。

How to resolve this?

如何解决这个问题?

import lombok.Getter;
import lombok.AllArgsConstructor

@AllArgsConstructor
public enum Direction {
    NORTH("NORTH"), // all these enums give error, for no constructor
    SOUTH("SOUTH"),
    EAST("EAST"),
    WEST("WEST");

    @Getter private String value;
}

P.S.: I am using intellij-idea, which has lombok plugin install. My lombok dependency version is: 1.16.20

PS:我使用的是intellij-idea,它安装了lombok 插件。我的 lombok 依赖版本是:1.16.20

回答by Martin Tlacha?

You also have to have enabled "Enable annotation processing" in Settings -> Build -> Compiler -> Annotation Processor

您还必须在 Settings -> Build -> Compiler -> Annotation Processor 中启用“启用注释处理”

回答by Henrique Ordine

That's usually an issue with your IDE, and strangely, it gets fixed after restarting the IDE. It's when the plugin lombok is not in the effect yet.

这通常是您的 IDE 的问题,奇怪的是,它在重新启动 IDE 后得到修复。这是插件 lombok 尚未生效的时候。