简体   繁体   English

Java Lombok库:继承的生成器批注问题

[英]Java Lombok library : builder annotation issues with Inheritance

Java Lombok library : builder annotation issues with Inheritance Java Lombok库:继承的生成器批注问题

    @Builder
    public class ParentClass {
        private final String a;
        private final String b;
    }

    @Builder
    public class ChildClass extends ParentClass{
        private final String c;
    }

When creating an instance of child class, parent class attributes are not visible with Builder annotations. 创建子类的实例时,父类属性在Builder批注中不可见。

Below fails: 以下失败:

     ChildClass.builder().a("testA").b("testB").c("testC").build();

However, below statement is correct: 但是,以下声明是正确的:

     ChildClass.builder().c("testC").build();

Seems this issue is open for long time, dont know if any latest release has any fixes. 似乎这个问题已经开放很长时间了,不知道任何最新版本是否有修复程序。

The latest lombok release 1.18.2 includes the new experimental @SuperBuilder . 最新的lombok版本1.18.2包括新的实验性@SuperBuilder It was added exactly for this: setting fields from superclasses. 正是为此添加的:从超类设置字段。

The problem is that when you define ParentClass with @Builder annotation it creates ParentClass(String) constructor and deletes implicit one. 问题在于,当您使用@Builder注释定义ParentClass时,它将创建ParentClass(String)构造函数并删除隐式构造函数。 Then Child class cannot even be created. 然后,甚至无法创建Child类。

Please look at the following answer: how to Call super constructor in Lombok 请查看以下答案: 如何在龙目岛中调用超级构造函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM