使用Lombok,是否可以指定一个arg构造函数?
我的目的是使用Lombok注释创建一个构造函数,如下所示.
class MyClass { private String param; private Integer count; public MyClass(String param) { this.param = param; } }
解决方法
I didn’t find in documentation
怎么样:http://projectlombok.org/features/Constructor.html?
您必须初始化所有不应该是构造函数的变量.
@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.
所以下面应该创建一个参数(param)构造函数:
@requiredArgsConstructor class MyClass { private String param; private Integer count = -1; }