简体   繁体   English

龙目岛继承忽略父级属性

[英]Lombok inheritance ignoring parent properties

Is it possible to have a child class ignoring some parent properties? 是否有可能使子类忽略某些父级属性? Something like: 就像是:

@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;
}

And then, when I want to create a new instance of Child ... 然后,当我想创建一个新的Child实例时...

int childId = 1;
String childName = "TEST";
int parentId = 15;
Child.of(childId, childName, parentId); //or other parameter order, doesnt matter...

I have also tried @Builder , but I have to provide the parents fields, such as: 我也尝试过@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;
    }
}

I do not fully understand what you try to achieve, but maybe the new experimental @SuperBuilder could help you. 我不完全了解您要达到的目标,但是也许新的实验性@SuperBuilder可以为您提供帮助。 Then you do not have to add constructors manually. 然后,您不必手动添加构造函数。 However, the parent's fields will still be settable via the builder, and the parent constructor will initialize them (possibly with null if you did not set them when building). 但是,仍可以通过构建器设置父级的字段,并且父级构造函数将对其进行初始化(如果在构建时未设置它们,则可能为null )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM