![](/img/trans.png)
[英]Lombok @Builder inheritance simultaneously working for parent and child
[英]Lombok inheritance ignoring parent properties
是否有可能使子类忽略某些父级属性? 就像是:
@Value(staticConstructor = "of")
public class Parent {
private final int parentId;
private final String parentName;
private final boolean isSomething;
}
@Value(staticConstructor = "of")
@EqualsAndHashCode(callSuper = false)
@JsonIgnoreProperties({"parentName", "isSomething"})
public class Child extends Parent {
private final int childId;
private final String childName;
}
然后,当我想创建一个新的Child
实例时...
int childId = 1;
String childName = "TEST";
int parentId = 15;
Child.of(childId, childName, parentId); //or other parameter order, doesnt matter...
我也尝试过@Builder
,但是我必须提供parent字段,例如:
@Value(staticConstructor = "of")
@EqualsAndHashCode(callSuper = false)
@JsonIgnoreProperties({"parentName", "isSomething"})
public class Child extends Parent {
private final int childId;
private final String childName;
@Builder
private Child(int parentId, int childId, String childName) {
super(parentId); //Error, need all the other parameters
this.childId = childId;
this.childName = childName;
}
}
我不完全了解您要达到的目标,但是也许新的实验性@SuperBuilder
可以为您提供帮助。 然后,您不必手动添加构造函数。 但是,仍可以通过构建器设置父级的字段,并且父级构造函数将对其进行初始化(如果在构建时未设置它们,则可能为null
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.