Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从超级解决方案访问子变量_Java_Oop_Inheritance - Fatal编程技术网

Java 从超级解决方案访问子变量

Java 从超级解决方案访问子变量,java,oop,inheritance,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 M

我有以下课程

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
    }
}
公共抽象类东西{
私有字符串外观;
公共void集合外观(字符串外观){
这个外观=外观;
}
}
公共抽象类MovableThing扩展了Thing{
//可移动对象向右移动的ASCII表示。
私有静态字符串面右;
//可移动对象向左移动的ASCII表示。
私有静态字符串面_左;
私人布尔右转;
公共动产{
设置外观(面向右);
正确=正确;
//其他一些事情
公共空间扭转(){
goingRight=!goingRight;
如果(向右){
设置外观(面向右);
}否则{
设置外观(面左);
}
}
公共类Bird扩展了MovableThing(){
私有静态字符串面_RIGHT=“/”/>;

private static String FACE_LEFT=“以下是我将如何使用您的代码来模拟您的场景:

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;
    }
}
公共抽象类东西{
私有字符串外观;
//要求Thing的子类具有定义的“向左”和“向右”
//方法。
公共摘要void setGoingLeft();
公共摘要void setGoingRight();
受保护的最终空集外观(字符串外观){
这个外观=外观;
}
}
公共抽象类MovableThing扩展了Thing{
私人布尔右转;
公共动产{
setGoingRight();
//其他一些事情
}
//要求子类定义一个方法,该方法给我一个字符串,显示
//他们面对的方式,当我告诉他们面对的方式时。这允许
//子类(如Bird)返回各自的外观,具体取决于
//他们面对的方式。
受保护的抽象字符串getAppearance(布尔右键);
//重写“向左”和“向右”方法(并使其成为最终方法)
//子类不能更改它们)。这些子类还可以修改
//动产是正确的。
@凌驾
公共最终无效设置Goingleft(){
正确=错误;
外观(假);
}
@凌驾
公共最终作废设置权(){
正确=正确;
外观(真实);
}
公共空间扭转(){
//如果他们向右转,将他们转过来会让他们向左转,反之亦然
//反之亦然。
如果(向右)
setGoingLeft();
其他的
setGoingRight();
}
}
公共类动产{
私有静态最终字符串面_RIGHT=“/”/>;

private static final String FACE_LEFT=“不能调用
Bird
类的构造函数
Chicken()
。请确保您的代码在发布前已编译。如果将
可移动对象
转换为
FACE\u RIGHT
有意义,则常量定义在错误的位置。@JimGarrison我正在为这一创意而努力……:D:D
可移动对象。FACE\u RIGHT
静态的
,您永远不会初始化它。
Bird.FACE\u RIGHT
静态的
,并且已初始化。这是两个独立的成员变量,一个不会隐藏另一个…您可能希望在
Bird
中使用
私有静态最终字符串FACE\u RIGHT
,但在
MovableThing
中使用
私有最终字符串FACE\u RIGHT
(无静态)。然后接受为args并在
MovableThing
构造函数中赋值。谢谢!我将有
Thing
的子类不能移动,所以我将只移动
setGoingLeft()
setGoingRight()
进入
MovableThing
?@unicorn407是的,
setGong…
方法应该在
MovableThing
中(因为它是移动的类),而
getAppearance
方法应该在
Thing
中(因为想必所有东西都会有某种外观)。请记住,在OOP中,您的目标通常是首先对事物进行分类,然后描述适合每个分类的所有事物的行为。