在 Lombok 中省略一个 Setter/Getter

发布于 2024-12-13 08:40:25 字数 136 浏览 4 评论 0原文

我想在 Lombok 中使用数据类。由于它有大约十几个字段,我用 @Data 对其进行注释,以生成所有的 setter 和 getter。但是,有一个特殊字段我不希望为其实现访问器。

如何让 Lombok 省略该字段?

I want to use a data class in Lombok. Since it has about a dozen fields, I annotated it with @Data in order to generate all the setters and getters. However, there is one special field for which I don't want the accessors to be implemented.

How do I make Lombok omit this field?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是男神闪亮亮 2024-12-20 08:40:25

您可以将访问级别传递给 @Getter@Setter 注释。这对于使 getter 或 setter 受到保护或私有非常有用。它还可用于覆盖默认值。

使用@Data,默认情况下您可以公开访问访问器。现在,您可以使用特殊访问级别 NONE 来完全省略访问器,如下所示:

@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private int mySecret;

You can pass an access level to the @Getter and @Setter annotations. This is useful to make getters or setters protected or private. It can also be used to override the default.

With @Data, you have public access to the accessors by default. You can now use the special access level NONE to completely omit the accessor, like this:

@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private int mySecret;
雨夜星沙 2024-12-20 08:40:25

根据 @Data 描述 您可以使用:

所有生成的 getter 和 setter 都将是公开的。要覆盖
访问级别,使用显式 @Setter 注释字段或类
和/或 @Getter 注释。您还可以使用此注释(通过
将其与 AccessLevel.NONE 结合起来)以抑制生成 getter
和/或完全设置器。

According to @Data description you can use:

All generated getters and setters will be public. To override the
access level, annotate the field or class with an explicit @Setter
and/or @Getter annotation. You can also use this annotation (by
combining it with AccessLevel.NONE) to suppress generating a getter
and/or setter altogether.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文