Java Lombok继承忽略父属性
是否可以让子类忽略某些父属性? 比如:Java Lombok继承忽略父属性,java,lombok,Java,Lombok,是否可以让子类忽略某些父属性? 比如: @Value(staticConstructor = "of") public class Parent { private final int parentId; private final String parentName; private final boolean isSomething; } @Value(staticConstructor = "of") @EqualsAndHashCode(callSuper =
@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
,但我必须提供父字段,例如:
@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
)这不是关于Lombok,而是关于Java,在Java中创建新的子对象时,首先调用父级构造函数,作为父级中的最终字段,您必须初始化主题