是否有可能使子类忽略某些父级属性? 像这样:
@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
,但是我必须提供parents字段,例如:
@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;
}
}
答案 0 :(得分:0)
我不太了解您要达到的目标,但是新的实验性@SuperBuilder
可能会为您提供帮助。然后,您不必手动添加构造函数。但是,仍然可以通过构建器设置父级字段,并且父级构造函数将对其进行初始化(如果构建时未设置,则可以使用null
进行初始化。)