java – Lombok:如何指定一个arg构造函数?

前端之家收集整理的这篇文章主要介绍了java – Lombok:如何指定一个arg构造函数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用Lombok,是否可以指定一个arg构造函数

我的目的是使用Lombok注释创建一个构造函数,如下所示.

  1. class MyClass {
  2. private String param;
  3. private Integer count;
  4.  
  5. public MyClass(String param) {
  6. this.param = param;
  7. }
  8. }

解决方法

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)构造函数

  1. @requiredArgsConstructor class MyClass {
  2. private String param;
  3. private Integer count = -1;
  4. }

猜你在找的Java相关文章