Java Project Lombok @Data 注释是否创建了任何类型的构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41979374/
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
Does the Project Lombok @Data annotation create a constructor of any kind?
提问by JoseHdez_2
I have a class with a @Data
annotation, but I'm not sure whether a constructor with arguments is generated or the only generated constructor is the default (no arguments) one from vanilla Java.
我有一个带@Data
注释的类,但我不确定是否生成了带参数的构造函数,或者唯一生成的构造函数是 vanilla Java 的默认(无参数)构造函数。
采纳答案by JoseHdez_2
A @RequiredArgsConstructor
will be generated if no constructor has been defined.
@RequiredArgsConstructor
如果未定义构造函数,则将生成A。
The Project Lombok @Data pageexplains:
该项目龙目岛@Data页解释:
@Data is like having implicit @Getter, @Setter, @ToString, @EqualsAndHashCode and @RequiredArgsConstructorannotations on the class (except that no constructor will be generated if any explicitly written constructor exists).
@Data 就像在类上具有隐式 @Getter、@Setter、@ToString、@EqualsAndHashCode 和@RequiredArgsConstructor注释(除了如果存在显式编写的构造函数,则不会生成构造函数)。
回答by Surbhi Gupta
@Data is only creating a @RequiredArgsConstructor. Lombok documentation site for the Data annotationand constructorsexplains:
@Data 只是创建一个 @RequiredArgsConstructor。数据注释和构造函数的Lombok 文档站点解释说:
@RequiredArgsConstructor generates a constructor with 1 parameter for each field that requires special handling. All non-initialized final fields get a parameter, as well as any fields that are marked as @NonNull that aren't initialized where they are declared. For those fields marked with @NonNull, an explicit null check is also generated. The constructor will throw a NullPointerException if any of the parameters intended for the fields marked with @NonNull contain null. The order of the parameters match the order in which the fields appear in your class.
@RequiredArgsConstructor 为每个需要特殊处理的字段生成一个带有 1 个参数的构造函数。所有未初始化的 final 字段都会获得一个参数,以及任何标记为 @NonNull 且未在声明位置初始化的字段。对于那些标有@NonNull 的字段,还会生成一个显式的空检查。如果用于标记为 @NonNull 的字段的任何参数包含 null,则构造函数将抛出 NullPointerException。参数的顺序与字段在您的类中出现的顺序相匹配。
Suppose you have a POJO that uses Lombok @Data annotation:
假设您有一个使用 Lombok @Data 注释的 POJO:
public @Data class Z {
private String x;
private String y;
}
You can't create the object as Z z = new Z(x, y) because there is no arg on your Z class that is "required". It is creating the constructor with zero parameters because @Data gives you Setters and Getters for your properties and you can call setX and setY after creating your instance.
您不能将对象创建为 Z z = new Z(x, y) 因为您的 Z 类上没有“必需”的 arg。它正在创建具有零参数的构造函数,因为@Data 为您的属性提供了 Setter 和 Getter,您可以在创建实例后调用 setX 和 setY。
You can either make x and y @NonNull or final so they must be passed through the constructor or annotate your class Z with @AllArgsConstructor.
您可以使 x 和 y @NonNull 或 final 使它们必须通过构造函数传递或使用 @AllArgsConstructor 注释您的类 Z。