在构造函数注释中使用 builder(lombok) 时出错

Error while use builder(lombok) in constructor annotation

@Data
@Builder
public static class Common {
    private String common1;
    private String common2;
}

@Getter
public static class Special extends Common {
    private String special1;

    @Builder
    public Special(String common1, String common2, String special1) {
        super(common1, common2);
        this.special1 = special1;
    }
}

出现以下错误:

Error:(149, 9) java: builder() in com.example.home.ExampleDTO.Special cannot override builder() in com.example.home.ExampleDTO.Common   
return type com.example.home.ExampleDTO.Special.SpecialBuilder is not compatible with com.example.home.ExampleDTO.Common.CommonBuilder

当我将 (builderMethodName = "b") 这个参数放入 @Builder(Special constructor) 然后 它工作正常。

@Builder(builderMethodName = "b")
public Special(String common1, String common2, String special1) {

我不知道为什么第一个代码会出错。 请帮帮我。 谢谢

@Builder 在两个 类 中创建一个静态方法 builder();它 return 是相应构建器的一个实例。但是方法的return类型不兼容,因为SpecialBuilderCommonBuilder不同且不相关类:@Builder不(技术上不能)考虑类之间的继承关系。所以编译器抱怨两个方法名称相同,没有参数,但 return 类型不同。这在 Java.

中是不可能的

要解决这个问题,您有两个选择:

  1. 对两个 类 使用 @SuperBuilder@SuperBuilder 设计用于继承。

  2. 如您所知,您可以在 类.

  3. 之一中重命名该方法