Lombok @builder on a class that extends another class

后端 未结 3 1966
一个人的身影
一个人的身影 2020-12-28 12:15

I have two classes Child extends Parent. I need to put @Builder annotation on the classes such that I do not need to create the builder my self.

3条回答
  •  暖寄归人
    2020-12-28 12:55

    See in https://blog.codecentric.de/en/2016/05/reducing-boilerplate-code-project-lombok/ (@Builder and inheritance part)

    Adjusted to your classes

    @AllArgsConstructor
    public class Parent {
      private double d;
      private float e;
    }
    
    public class Child extends Parent {
      private String a;
      private int b;
      private boolean c;
    
      @Builder
      public Child(String a, int b, boolean c, double d, float e) {
        super(d, e);
        this.a = a;
        this.b = b;
        this.c = c;
      }
    }
    

    With this setup

    Child child = Child.builder().a("aVal").b(1000).c(true).d(10.1).e(20.0F).build();
    

    works correctly

提交回复
热议问题