Lombok @Builder继承解决方法 [英] Lombok @Builder inheritance workaround

查看:6761
本文介绍了Lombok @Builder继承解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Lombok @Builder不适用于继承用例:

Lombok @Builder doesn't work for inheritance use cases:

例如

class Foo{
 protected int xyz1;
 .....
 protected String xyz7;
}


class Bar extends Foo{

}

对于给定的用例,Lombok将无法生成设置Foo类中定义的参数值的方法。

For given use case Lombok will not be able to generate methods to set value of parameter defined in Foo class.

解决方法是:


  1. 手动创建Bar的构造函数。

  2. 在构造函数上放置Builder注释。

有更好的解决方法吗?

Is there a better workaround ?

推荐答案

有点隐藏,但人们之前有过这个问题,请参阅:

It´s a bit hidden, but people have had this question before, see:

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()

这篇关于Lombok @Builder继承解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆