带有自定义分配的RequiredArgsConstructor
我很好奇是否可以在构造函数调用期间使用 @RequiredArgsConstructor 分配自定义值。即
private String value1;
private String value2;
private String value3;
public MyConstructor(String value1, String value2) {
this.value1 = value1;
this.value2 = value2;
this.value3 = createString();
}
相反,我会寻找:
@RequiredArgsConstructor
public class MyConstructor {
@NonNull
private String value1;
@NonNull
private String value2;
private String value3;
private String createString() {
return "test";
}
}
I'm curious if it's possible to assign a custom value during a constructor call using @RequiredArgsConstructor. I.e.
private String value1;
private String value2;
private String value3;
public MyConstructor(String value1, String value2) {
this.value1 = value1;
this.value2 = value2;
this.value3 = createString();
}
Instead I'll be looking for:
@RequiredArgsConstructor
public class MyConstructor {
@NonNull
private String value1;
@NonNull
private String value2;
private String value3;
private String createString() {
return "test";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以直接在属性的定义中分配
createString()
。因此,当创建新实例时,value3
也会被初始化。You can assign
createString()
directly in the definition of the attribute. Therefore, when a new instance is created, also thevalue3
is initialized.