如何为抽象类实现Lombok @Builder [英] How to implements Lombok @Builder for Abstract class

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

问题描述

我有一些扩展抽象类的类,并且我不想将@Builder放在所有子类的顶部.

I have classes that extend an abstract class and I don't want to put @Builder on top of the all child classes.

是否可以为抽象类实现Lombok @Builder?

Is there any way to implement Lombok @Builder for an abstract class?

推荐答案

完全不可能.生成器在编译期间生成到超类中,并且它对最终实现它的可能子类不了解.

Not possible at all. The builder is generated into the super class during compiling and it can not have any knowledge of the possible sub classes that will eventually implement it.

例如,子类可能具有必须用于实例的构造函数,以使其具有有效状态,而Lombok在生成构建器时对此一无所知.

For example, the sub class could have constructors that have to be used for the instance to have a valid state and Lombok can not have any knowledge about it when the builder is generated.

看看@Builder文档中的示例代码.您很快就会发现,将它适配成实例化未知子类是不可能的.

Take a look at the example code in @Builder documentation. You'll quickly see that it is just impossible to adapt it into instantiating an unknown sub class.

这篇关于如何为抽象类实现Lombok @Builder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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