Java Lombok 中各种构造函数的注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25809156/
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
Annotation for various constructors in Lombok?
提问by barbara
I have a class
我有一堂课
public class Answer<T> {
private T data;
public Answer(T data) {
this.data = data;
}
public Answer() {
}
public T getData() {
return data;
}
public Answer<T> setData(T data) {
this.data = data;
return this;
}
}
which I want to simplify with Lombok
.
我想用Lombok
.
If I add annotation @AllArgsConstructor
than I can't see default constructor.
如果我添加注释@AllArgsConstructor
,则看不到默认构造函数。
@Data
@AllArgsConstructor
public class Answer<T> {
private T data;
public Answer<T> setData(T data) {
this.data = data;
return this;
}
}
Is it possible to have both constructor in Lombok
?
是否可以同时拥有两个构造函数Lombok
?
采纳答案by Boris the Spider
Your class is equivalent to:
你的班级相当于:
@Accessors(chain = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Answer<T> {
private T data;
}
Although strictly speaking this adds toString
, equals
and hashCode
methods on allvariables. This can (and often does) cause infinite loops. Be very wary of @Data
.
尽管严格来说这会在所有变量上添加toString
,equals
和hashCode
方法。这会(并且经常会)导致无限循环。非常小心。@Data
@Accessors(chain = true)
makes the setter
implementations return this
, more info here.
@Accessors(chain = true)
使setter
实现返回this
,更多信息在这里。
You can add multiple constructor annotations:
您可以添加多个构造函数注释:
Unlike most other lombok annotations, the existence of an explicit constructor does not stop these annotations from generating their own constructor.
与大多数其他 lombok 注释不同,显式构造函数的存在不会阻止这些注释生成自己的构造函数。
Note that @Accessors
is experimental so may be changed/renamed at some future point.
请注意,这@Accessors
是实验性的,因此可能会在未来某个时候更改/重命名。
I prefer @Builder
to @AllArgsConstructor
as it allows only requiredparameters to be set, whereas a all arguments constructor is all or nothing. It also generates much more readable code, consider
我更喜欢@Builder
,@AllArgsConstructor
因为它只允许设置必需的参数,而所有参数构造函数要么全有要么全无。它还生成更具可读性的代码,请考虑
new Thing(true, 1, 4, false, 4, 4.0)
Versus
相对
new Thing.Builder().
setANamnedProperty(true).
setAnotherNamnedProperty(1).
....
build();
回答by vikingsteve
Have you tried this?
你试过这个吗?
@NoArgsConstructor
@AllArgsConstructor
回答by kris
Try this. This should work. Add this annotation as well.
尝试这个。这应该有效。也添加此注释。
@NoArgsConstructor
@NoArgsConstructor
@NoArgsConstructor will generate a constructor with no parameters. If this is not possible (because of final fields), a compiler error will result instead, unless @NoArgsConstructor(force = true) is used, then all final fields are initialized with 0 / false / null. For fields with constraints, such as @NonNull fields, no check is generated,so be aware that these constraints will generally not be fulfilled until those fields are properly initialized later. Certain java constructs, such as hibernate and the Service Provider Interface require a no-args constructor. This annotation is useful primarily in combination with either @Data or one of the other constructor generating annotations.
@NoArgsConstructor 将生成一个没有参数的构造函数。如果这是不可能的(因为 final 字段),则会导致编译器错误,除非使用 @NoArgsConstructor(force = true),然后所有 final 字段都初始化为 0 / false / null。对于具有约束的字段,例如@NonNull 字段,不会生成检查,因此请注意,在稍后正确初始化这些字段之前,通常不会满足这些约束。某些 java 构造,例如 hibernate 和服务提供者接口,需要无参数构造函数。此注释主要与 @Data 或其他生成注释的构造函数之一结合使用。
回答by Ilya Budu
Yes, you can use both constructor in Lombok.
是的,您可以在 Lombok 中使用这两个构造函数。
@NoArgsConstructor
@AllArgsConstructor
@AllArgsConstructorgenerates a constructor requiring an argument for every field in the annotated class.
@AllArgsConstructor生成一个构造函数,需要为带注释的类中的每个字段提供一个参数。
So we have the Employee class with two fields:
所以我们有包含两个字段的 Employee 类:
@AllArgsConstructor
public class Employee {
private String name;
private int salary;
}
When we de-lombok the class, it becomes:
当我们 de-lombok 类时,它变成:
public class Employee {
private String name;
private int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
}
@NoArgsConstructorgenerates a default constructor with no parameters.
@NoArgsConstructor生成一个没有参数的默认构造函数。
We have the following Employee class:
我们有以下 Employee 类:
@NoArgsConstructor
public class Employee {
private String name;
private int salary;
}
When we look at the generated code, we see that Lombok adds a no-args constructor:
当我们查看生成的代码时,我们看到 Lombok 添加了一个无参数构造函数:
public class Employee {
private String name;
private int salary;
public Employee() {
}
}