【发布时间】:2018-12-09 20:50:48
【问题描述】:
我有这个简单的代码:
@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 注释,一切都很好。但是我添加了构造函数,代码不再编译。错误是:
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
所以我有两个问题:
- 为什么 Lombok
@Builder与此构造函数不兼容? - 考虑到我需要构建器和构造器,如何编译代码?
【问题讨论】:
-
从错误中,我假设 1) 构造函数中的参数数量不正确 2) 传递给构造函数的参数类型不正确
-
所有构造函数或特定构造函数都会发生这种情况吗?即使更改构造函数的参数也会报错吗?
-
@wdc 我只有一个构造函数(代码中提到了)。我需要带有此参数的构造函数才能复制对象。
-
你检查
@Builder(toBuilder = true)了吗?这假设为您提供复制构造函数的功能。Foo copy = original.toBuilder().build() -
@wdc 刚刚尝试了您的解决方案。使用构造函数会更好。谢谢!