从超级变通方法

时间:2018-06-10 21:53:45

标签: java oop inheritance

我有以下课程。

public abstract class Thing {
    private String appearance;

    public void setAppearance(String appearance) {
        this.appearance = appearance;
    }
}

public abstract class MovableThing extends Thing {
    // ASCII representation of MovableThing moving right.
    private static String FACE_RIGHT;

    // ASCII representation of MovableThing moving left.
    private static String FACE_LEFT;

    private boolean goingRight;

    public MovableThing() {
        setAppearance(FACE_RIGHT);
        goingRight = true;
        // Some other things

    public void turnAround() {
        goingRight = !goingRight;
        if (goingRight) {
            setAppearance(FACE_RIGHT);
        } else {
            setAppearance(FACE_LEFT);
        }
    }

public class Bird extends MovableThing() {
    private static String FACE_RIGHT = "/'/>";
    private static String FACE_LEFT = "<\\'\\";

    public Bird() {
        super();
        // Some other things
    }
}

我知道目前这是不正确的,因为在MovableThing中,FACE_RIGHT没有被分配任何内容,所以当我在super()中呼叫Bird时,{{1}只是设置为appearance。我该如何解决这个问题?我有多个具有不同左/右ASCII表示的动物,但我不确定如何以OOP方式完成所有这些。

修改:要说null而不是Bird()

1 个答案:

答案 0 :(得分:0)

以下是我将使用您的代码对您的方案进行建模的方法:

public abstract class Thing {
    private String appearance;

    // Require subclasses of Thing to have a defined "going left" and "going right"
    // method.
    public abstract void setGoingLeft();

    public abstract void setGoingRight();

    protected final void setAppearance(String appearance) {
        this.appearance = appearance;
    }
}

public abstract class MovableThing extends Thing {

    private boolean goingRight;

    public MovableThing() {
        setGoingRight();
        // Some other things
    }

    // Require subclasses to define a method that gives me a String showing which
    // way they're facing, when I tell them what way they're facing. This allows
    // subclasses (like Bird) to each return their own appearances depending on the
    // way they are facing.
    protected abstract String getAppearance(boolean right);

    // Override the "going left" and "going right" methods (and make them final so
    // subclasses can't change them). These also modify the "goingRight" field of a
    // MovableThing correctly.
    @Override
    public final void setGoingLeft() {
        goingRight = false;
        getAppearance(false);
    }

    @Override
    public final void setGoingRight() {
        goingRight = true;
        getAppearance(true);
    }

    public void turnAround() {
        // If they're going right, turning them around will make them go left and vice
        // versa.
        if (goingRight)
            setGoingLeft();
        else
            setGoingRight();
    }
}

public class Bird extends MovableThing {

    private static final String FACE_RIGHT = "/'/>";
    private static final String FACE_LEFT = "<\\'\\";

    // This method is called by the super class.
    @Override
    protected String getAppearance(boolean right) {
        // If the super class asks for a Bird's appearance when facing right, return
        // "FACE_RIGHT". Otherwise, return "FACE_LEFT". (Other animals can return
        // different things depending on the way they're facing.)
        return right ? FACE_RIGHT : FACE_LEFT;
    }
}