Lombok @Builder不适用于继承用例:
例如
class Foo{
protected int xyz1;
.....
protected String xyz7;
}
class Bar extends Foo{
}
对于给定的用例,Lombok将无法生成设置Foo类中定义的参数值的方法。
解决方法是:
有更好的解决方法吗?
答案 0 :(得分:11)
Lombok引入了实验性功能,其版本为1.18.2,用于解决Builder注释所面临的继承问题,并且可以使用@SuperBuilder注释进行解决,如下所示。
@SuperBuilder
public class ParentClass {
private final String a;
private final String b;
}
@SuperBuilder
public class ChildClass extends ParentClass{
private final String c;
}
现在,可以使用如下所示的Builder类(使用@Builder注释是不可能的)
ChildClass.builder().a("testA").b("testB").c("testC").build();
答案 1 :(得分:8)
有点隐藏,但人们之前有过这个问题,请参阅:
https://reinhard.codes/2015/09/16/lomboks-builder-annotation-and-inheritance/
总结博客文章
@AllArgsConstructor
public class Parent {
private String a;
}
public class Child extends Parent {
private String b;
@Builder
private Child(String a, String b){
super(a);
this.b = b;
}
}
允许你使用
Child.builder().a("testA").b("testB").build()
答案 2 :(得分:0)
目前正在解决这个问题。在此处查看进度:https://github.com/rzwitserloot/lombok/pull/1337