[英]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.