Lombok @builder在扩展另一个类的类上 [英] Lombok @builder on a class that extends another class

查看:1412
本文介绍了Lombok @builder在扩展另一个类的类上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类 Child extends 。我需要在这些类上添加@Builder注释,这样我就不需要为我自己创建构建器。

  package jerry; //内部编译器错误:java.lang.NullPointerException 

import lombok.AllArgsConstructor;
import lombok.Builder;

@AllArgsConstructor(onConstructor = @ __(@ Builder))
public class Child extends Parent {
//此行的多个标记
// - 隐式超级构造函数Parent()未定义。必须显式调用另一个构造函数
// - 覆盖java.lang.Object.toString

private String a;
private int b;
private boolean c;

}


@Builder
public class Parent {
private double d;
私人浮动e;
}

我需要能够构建子实例



pre $ Child child = Child.builder()。a(aVal)。b(1000).c(true).d(10.1)。 E(20.0F).build();

但是到目前为止,我收到了代码注释中提到的错误。任何人都可以指出我如何通过lombok或任何其他类似的图书馆来实现它吗?

子问题



为什么 @AllArgsConstructor(onConstructor = @ __(@ Autowired))编译但是 @AllArgsConstructor(onConstructor = @ __(@ Builder))不是? https://blog.codecentric.de/en/2016/05/reducing -boilerplate-code-project-lombok / ( @Builder和inheritance 部分)



调整到您的课程

  @AllArgsConstructor 
public class Parent {
private double d;
私人浮动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;


$ / code $ / pre
$ b $ p

使用此设置

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

正常工作


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.

package jerry;// Internal compiler error: java.lang.NullPointerException

import lombok.AllArgsConstructor;
import lombok.Builder;

@AllArgsConstructor(onConstructor=@__(@Builder))
public class Child extends Parent { 
//Multiple markers at this line
//  - Implicit super constructor Parent() is undefined. Must explicitly invoke another constructor
//  - overrides java.lang.Object.toString

   private String a;
   private int b;
   private boolean c;

}


@Builder
public class Parent {
    private double d;
    private float e;
}

I need to be able to build child instance such that

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

But so far I am getting the errors mentioned in side that code comments. Can any one point me to the right direction how to achieve it with lombok or any other similar library ?

Sub-question

Why does @AllArgsConstructor(onConstructor=@__(@Autowired)) compile but @AllArgsConstructor(onConstructor=@__(@Builder)) does not?

解决方案

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

这篇关于Lombok @builder在扩展另一个类的类上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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