本文介绍了为什么Lombok @Builder与该构造函数不兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的代码:

@Data
@Builder
public class RegistrationInfo {

    private String mail;
    private String password;

    public RegistrationInfo(RegistrationInfo registrationInfo) {
        this.mail = registrationInfo.mail;
        this.password = registrationInfo.password;
    }
}

首先,我只使用了@Builder Lombok批注,一切都很好.但是我添加了构造函数,并且代码不再编译.错误是:

First I was using only the @Builder Lombok annotation and everything was fine. But I added the constructor and the code does not compile any more. The error is:

Error:(2, 1) java: constructor RegistrationInfo in class com.user.RegistrationInfo cannot be applied to given types;
  required: com.user.RegistrationInfo
  found: java.lang.String,java.lang.String
  reason: actual and formal argument lists differ in length

所以我有两个问题:

  1. 为什么Lombok @Builder与该构造函数不兼容?
  2. 考虑到我既需要构建器又需要构造器,如何使代码编译?
  1. Why is Lombok @Builder not compatible with this constructor?
  2. How do I make the code compile taking into account that I need both the builder and the constructor?

推荐答案

您可以添加@AllArgsConstructor批注,因为

(引用@Andrew Tobilko)

(Quotting @Andrew Tobilko)

或将属性设置为@Builder:@Builder(toBuilder = true)这为您提供了复制构造函数的功能.

Or set an attribute to @Builder : @Builder(toBuilder = true) This gives you the functionality of a copy constructor.

@Builder(toBuilder = true)
class Foo {
    // fields, etc
}

Foo foo = getReferenceToFooInstance();
Foo copy = foo.toBuilder().build();

这篇关于为什么Lombok @Builder与该构造函数不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:32