简体   繁体   中英

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 ...

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:

@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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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