在方法重写中返回继承的类而不是超类 [英] returning inherited class instead of superclass in method overriding

查看:88
本文介绍了在方法重写中返回继承的类而不是超类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特定的类结构,看起来像这样:

I have a certain class structure that looks something like this:

class Parent {
    public Parent(int property) { /* use property */}
}
class Son extends Parent {
    public Son(int parentProperty, String sonProperty) { 
        super(parentProperty);
        /* use son property */ 
    }
}

我想为这两个类创建构建器,使得:

I'd like to create builders for both these classes such that:

class ParentBuilder {
    protected int parentProperty;

    public ParentBuilder parentProperty(int parentPropertyValue) {
        parentPropertyValue = parentPropertyValue;
        return this;
    }

    public Parent build() {
        return new Parent(parentProperty);
    }
}
class SonBuilder extends ParentBuilder {
    private String sonProperty;

    public SonBuilder sonProperty(String sonProperty) {
        this.sonProperty = sonProperty;
        return this;
    }

    @Override
    public Son build() {
        return new Son(parentProperty, sonProperty);
    }
}

但这会导致以下问题:

SonBuilder sonBuilder = new SonBuilder();
sonBuilder.sonProperty("aString").build(); // this works and creates Son
sonBuilder.sonProperty("aString").parentProperty(1).build(); // this works and creates Parent instead of Son
sonBuilder.parentProperty(1).sonProperty("aString").build(); // this doesn't work

我意识到我很挑剔,这可以通过不返回this(即不使用方法链)来解决,但是我想知道是否有一个优雅的解决方案.

I realize I'm nitpicking and this could be solved with just not returning this (i.e. without method chaining), but I'm wondering if there is an elegant solution.

修改

优雅"一词似乎引起了一些混乱.

It seems that the word "elegant" is a source for a bit of confusion.

优雅"是指一种允许方法链接且不涉及强制转换的解决方案.

By "elegant" I mean a solution which allows for method chaining and does not involve casting.

推荐答案

第一点

sonBuilder.sonProperty("aString").parentProperty(1).build();

这有效并创建了Parent而不是Son

this works and creates Parent instead of Son

预期是parentProperty()返回ParentBuilder的原因:

public ParentBuilder parentProperty(int parentPropertyValue) {...

然后ParentBuilder.build()创建一个Parent:

public Parent build() {
    return new Parent(parentProperty);
}

第二点

sonBuilder.parentProperty(1).sonProperty("aString").build(); // this doesn't work

如第一点所述,parentProperty()返回一个ParentBuilder.
而且ParentBuilder当然没有sonProperty()方法.
因此它无法编译.

As said in the first point, parentProperty() returns a ParentBuilder.
And ParentBuilder of course doesn't have a sonProperty() method.
So it cannot compile.

我想知道是否有一个优雅的解决方案.

I'm wondering if there is an elegant solution.

一个优雅的解决方案不是让SonBuilder继承ParentBuilder而是由ParentBuilder字段组成. 例如:

An elegant solution would be not make SonBuilder inherited ParentBuilder but compose with a ParentBuilder field. For example :

class SonBuilder {

    private String sonProperty;
    private ParentBuilder parentBuilder = new ParentBuilder();

    public SonBuilder sonProperty(String sonProperty) {
      this.sonProperty = sonProperty;
      return this;
    }

    public SonBuilder parentProperty(int parentPropertyValue) {
      parentBuilder.parentProperty(parentPropertyValue);
      return this;
    }

    public Son build() {
      return new Son(parentBuilder.parentProperty, sonProperty);
    }
}

您可以这样创建Son:

SonBuilder sonBuilder = new SonBuilder();
Son son = sonBuilder.sonProperty("aString").parentProperty(1).build();

这篇关于在方法重写中返回继承的类而不是超类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆