我有这个简单的代码:
@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
与这个构造函数不兼容? - 如果需要同时使用构建器和构造函数,该怎么编译代码?
@Builder(toBuilder = true)
了吗?它应该给你复制构造函数的功能。Foo copy = original.toBuilder().build()
- wdc