Igo*_*kov 1 java inheritance builder lombok
我已经阅读了这篇关于将 lombok@Bulider与继承结合使用的文章https://reinhard.codes/2015/09/16/lomboks-builder-annotation-and-inheritance/
一切正常。但在我的情况下,我还需要使用 builder for Parent class,而这种解决方法不起作用。
我也尝试添加@Builder,Parent class但编译失败,因为Child class尝试覆盖builder()父级的方法。
@AllArgsConstructor
public class Parent {
private final long a;
private final long b;
private final double c;
}
public class Child extends Parent{
private final long aa;
private final long bb;
private final double cc;
@Builder
public Child(long a, long b, long c,
long aa, long bb, long cc)
super(a,b,c);
this.aa = aa;
this.bb = bb;
this.cc =cc;
}
Run Code Online (Sandbox Code Playgroud)
我需要两个案例生成器,例如:
Parent.builder().a(10).b(20).build();
Child.builder().a(10).aa(20).bb(100).build();
Run Code Online (Sandbox Code Playgroud)
lombok 可以处理这种情况吗?
Lombok 尝试覆盖builder()Child 中 Parent 类的方法。所以我试图将非默认名称设置为 builder 方法。
@Builder(builderMethodName = "parentBuilder")
@AllArgsConstructor
public class Parent {
private final long a;
private final long b;
private final double c;
}
public class Child extends Parent{
private final long aa;
private final long bb;
private final double cc;
@Builder(builderMethodName = "childBuilder")
public Child(long a, long b, long c,
long aa, long bb, long cc)
super(a,b,c);
this.aa = aa;
this.bb = bb;
this.cc =cc;
}
Run Code Online (Sandbox Code Playgroud)
这对我行得通。
| 归档时间: |
|
| 查看次数: |
2642 次 |
| 最近记录: |