Доступ к дочерним переменным из супер обходного пути - PullRequest
0 голосов
/ 11 июня 2018

У меня есть следующие классы.

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, appearance просто устанавливаетсядо null.Как я могу обойти это?У меня есть несколько животных с разными представлениями левого / правого ASCII, но я не уверен, как сделать все это в виде ООП.

Редактировать: Значит сказать Bird() вместо Chicken().

1 Ответ

0 голосов
/ 11 июня 2018

Вот что я хотел бы сделать с вашим кодом для моделирования вашего сценария:

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;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...