构造器的调用问题,继承方法的覆盖问题

package main;

class Glyph{
    void draw() {
        System.out.println("Glyph.draw()");
    }
    Glyph(){
        System.out.println("Glyph() before draw()");
        draw();
        System.out.println("Glyph() after draw()");
    }
}

class RoundGlyph extends Glyph{
    private int radius = 1;
    public RoundGlyph(int r) {
        radius = r;
        System.out.println("RoundGlyph");
    }
    void draw() {
        System.out.println("RoundGlyph.draw()");
    }
}

public class test {

    public static void main(String[] args) {
        new RoundGlyph(4);
    }

}


输出为:
Glyph() before draw()
RoundGlyph.draw()
Glyph() after draw()
RoundGlyph
为什么基类构造器调用的draw()方法是RoundGlyph的,难道在基类构造器中的调用已经发生了导出类draw的重写了吗?

坦率地说,这种程序在开发中没有任何实际的意义,应该被禁止。
因为基类构造函数在派生类的构造函数之前被调用。而构造函数被假定是最早被执行的。
成员函数比构造函数早执行,这种情况带来很多未知的后果。

你的困惑很正常——这是语法没有禁止,但是程序员应该避免的写法。

你实例化的子类,子类中已经将draw()重写,执行的时候会执行最底层子类的重写方法。