为必需的超级字段生成构造函数 [英] Generate constructor for required super fields
本文介绍了为必需的超级字段生成构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下课程:
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public abstract class EmailData {
private final Iterable<String> recipients;
}
和以下子类:
import lombok.Getter;
@Getter
public class PasswordRecoveryEmail extends EmailData {
private final String token;
}
是否有可能以这样的方式对 PasswordRecoveryEmail
进行注释,以便生成必需的类和超类字段的构造函数?
Is there any possibility to annotate PasswordRecoveryEmail
in such a way that a constructor for both required class and superclass fields will be generated?
推荐答案
@…Constructor
注释将不会显式调用构造函数,因此它们都依赖于默认的构造函数来完成适当的工作.因此,不,您不能说服Lombok为您创建这些构造函数.
The @…Constructor
annotations will not explicitly call a constructor, so they all rely on a default constructor doing a proper job. So, no, you cannot convince Lombok to create these constructors for you.
您能获得的最接近的一个是:
The closest you can get it is to either:
- 在
EmailData
中提供受保护
的默认构造函数(无参数),并为收件人
分配一些合理的值. - 自己编写
PasswordRecoveryEmail
的required-args构造函数.
- Provide a default constructor (no arguments) in
EmailData
that isprotected
and assigns some reasonable value torecipients
. - Write the required-args constructor for
PasswordRecoveryEmail
yourself.
在我的经验中,龙目岛常常无法完全继承.
Inheritance often is not quite covered by Lombok in my experience.
这篇关于为必需的超级字段生成构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文