Java Lombok:省略@AllArgsConstructor 中的一个字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23761242/
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-14 01:05:09  来源:igfitidea点击:

Java Lombok: Omitting one field in @AllArgsConstructor?

javasyntaxconstructorfieldlombok

提问by user3656823

If I specify @AllArgsConstructorusing Lombok, it will generate a constructor for setting all the declared (not final, not static) fields. Is it possible to omit some field and this leave generated constructor for all other fields?

如果我使用Lombok指定@AllArgsConstructor,它将生成一个构造函数来设置所有声明的(非最终的,非静态的)字段。是否可以省略某个字段,而这会为所有其他字段生成构造函数?

采纳答案by Roel Spilker

No that is not possible. There is a feature request to create a @SomeArgsConstructor where you can specify a list of involved fields.

不,那是不可能的。有一个功能请求创建一个@SomeArgsConstructor,您可以在其中指定相关字段的列表。

Full disclosure: I am one of the core Project Lombok developers.

完全披露:我是龙目岛项目的核心开发人员之一。

回答by dermoritz

Alternatively, you could use @RequiredArgsConstructor. This adds a constructor for all fields that are either @NonNullor final.

或者,您可以使用@RequiredArgsConstructor. 这为所有为@NonNull或 的字段添加了一个构造函数final

See the documentation

查看文档

回答by enkara

A good way to go around it in some cases would be to use the @Builder

在某些情况下,解决它的一个好方法是使用 @Builder