繁体   English   中英

Java超级方法调用子类型重写方法

[英]Java super method call in a subtype overridden method

可能有人请解释我为什么如果我运行此代码输出是[4,2]:null而不是[4,2]:紫色? 我的理解是问题在于superClass中的toString方法。 事实上,如果我从SuperClass中的toString中删除“final”并编写一个类似的toString方法

    public String toString() {
        return this.makeName();
    } 

在子类中一切正常。 但我真的不明白这背后的概念。 是否有关于此的内容?

感谢您的时间。

public class Point { 
    protected final int x, y;
    private final String name;

    public Point(int x, int y) { 
        this.x = x;
        this.y = y;
        name = makeName();
    }
    protected String makeName() { 
        return "["+x+", "+y+"]";
    }
    public final String toString(){ 
        return name;
    } 
}

ColorPoint.java:

public class ColorPoint extends Point { 
    private final String color;

    public ColorPoint(int x,int y, String color) { 
        super(x, y);
        this.color = color;
    }
    protected String makeName() {
        return super.makeName() + ":" + color;
    }
    public static void main(String[] args) { 
        System.out.println(new ColorPoint(4, 2, "purple"));
    } 
}

当你这样做时:

super(x, y);

它调用超类的构造函数,因此调用makeName()方法(由于行name = makeName(); )。

由于您在子类中重新定义它,因此它会调用它,但此时未定义颜色。

因此return super.makeName() + ":" + color; 等价于return super.makeName() + ":" + null;

因此,执行流程在以下(简化)中是等效的:

new ColorPoint(4, 2, "purple") //<-- creating ColorPoint object
super(x, y); //<-- super call
this.x = 4; 
this.y = 2;
name = makeName(); //<-- call makeName() in your ColorPoint class
return super.makeName() + ":" + color; //<-- here color isn't defined yet so it's null
name = "[4, 2]:null";
color = "purple";
/***/
print [4, 2]:null in the console //you call the toString() method, since it returns name you get the following output


请注意,您可以使用以下内容简化类:

 class Point { protected final int x, y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public String toString() { return "["+x+", "+y+"]"; } } class ColorPoint extends Point { private final String color; public ColorPoint(int x,int y, String color) { super(x, y); this.color = color; } @Override public String toString() { return super.toString() + ":" + color; } } 

你的toString()方法返回变量name的值。 通过调用重写的方法makeName() ,在超类的构造函数期间填充此变量。

此时,子类的变量color尚未填充,因此它正确返回[4,2]:null

在将值赋给color之前调用super(int,int),因此在color具有值之前调用makeName(),因此:null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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