我以为我可以像普通的Java构造函数一样通过args覆盖构造函数上的构造函数。
但是IDE IntelliSense不会建议我想要的方法。
构造函数上的龙目岛建筑应该只是一个吗?
这是我的代码:
_masterView
@Builder(builderMethodName = "builderByDto")
public SourceInfo(SourceInfoDto sourceInfoDto) {
this.sourceTitle = sourceInfoDto.getSourceTitle();
this.isbn = sourceInfoDto.getIsbn();
this.eissn = sourceInfoDto.getEissn();
this.issn = sourceInfoDto.getIssn();
this.volume = sourceInfoDto.getVolume();
this.pages = sourceInfoDto.getPages();
this.issue = sourceInfoDto.getIssue();
this.publishedYear = sourceInfoDto.getPublishedYear();
this.publishedDate = sourceInfoDto.getPublishedDate();
}
@Builder(builderMethodName = "builderByLiteRecord")
public SourceInfo(LiteRecordDto liteRecordDto) {
Map<String, String> source = liteRecordDto.getSource();
this.sourceTitle = source.get("sourceTitle");
this.isbn = source.get("isbn");
this.eissn = source.get("eissn");
this.issn = source.get("issn");
this.volume = source.get("volume");
this.pages = source.get("pages");
this.issue = source.get("issue");
this.publishedYear = source.get("publishedYear");
this.publishedDate = source.get("publishedDate");
}
答案 0 :(得分:0)
如果需要两个基于构造函数的构建器,则还需要两个单独的构建器类。您可以按如下方式定制构建器类:
@Builder(builderClassName = "BuilderByLiteRecord", builderMethodName = "builderByLiteRecord")
public SourceInfo(LiteRecordDto liteRecordDto) {
...
PS:至少从您在示例中所显示的来看,构建器模式似乎并不适合此处,至少不适用于构造函数。
我只将@Builder
放在类上,以便获得带有所有字段的生成器类。然后使用两种静态方法,每种方法都将您的DTO类之一作为参数,并返回一个填充有DTO数据的构建器。