Java 用于布尔字段的 Lombok 注释 @Getter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42619986/
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
Lombok annotation @Getter for boolean field
提问by Nitesh Kumar
I am using Java lombok annotation @Getter to generate getters for my POJO. I have a 'boolean' field by the name 'isAbc'. The @Getter annotation in this case generates a method by the name 'isAbc()'. Shouldn't it generate a method by the name 'isIsAbc()'?
我正在使用 Java lombok 注释 @Getter 为我的 POJO 生成 getter。我有一个名为“isAbc”的“布尔值”字段。在这种情况下,@Getter 注释生成一个名为“isAbc()”的方法。它不应该生成一个名为“isIsAbc()”的方法吗?
采纳答案by Harald Gliebe
Read the 'small print' section on the lombok page https://projectlombok.org/features/GetterSetter.html
阅读 lombok 页面上的“小字”部分https://projectlombok.org/features/GetterSetter.html
For boolean fields that start with is immediately followed by a title-case letter, nothing is prefixed to generate the getter name.
对于以 开头的布尔字段,后面紧跟标题大小写字母,没有任何前缀来生成 getter 名称。
So the behavior you experience is as specified.
所以你所经历的行为是指定的。
Note that the behavior is different for boolean
and Boolean
:
需要注意的是,行为是不同的boolean
和Boolean
:
@Getter
private boolean isGood; // => isGood()
@Getter
private boolean good; // => isGood()
@Getter
private Boolean isGood; // => getIsGood()
回答by Sunny
I do some tests against the lombok(1.16.8), and the conclusions are as below.
我对lombok(1.16.8)做了一些测试,结论如下。
private Boolean good;
getter => getGood() Boolean
setter => setGood(Boolean good) void
private boolean good;
getter => isGood() boolean
setter => setGood(boolean good) void
private Boolean isGood;
getter => getIsGood() Boolean
setter => setIsGood() void
private boolean isGood;
getter => isGood() boolean
setter => setGood(boolean good) void