构造器内部的多态方法的行为

package thinking;

import static java.lang.System.out;

/**
 * @USER echo
 * @TIME 2017-07-23 16:58
 * @DESC 构造器内部的多态方法的行为
 **/
class Glyph {
    void draw() {
        out.println("Glyph.draw()");
    }
    Glyph() {
        out.println("Glyph() before draw()");
        draw();
        out.println("Glyph() after draw()");
    }
}
class RoundGlyph extends Glyph {
    private int redis = 2;
    RoundGlyph(int r) {
        redis = r;
        out.println("RoundGlyph.RoundGlyph().redis = " + redis);
    }
    void draw() {
        out.println("RoundGlyph.draw().redis = " + redis);
    }

}

/**
 * 初始化的实际过程是:
 *
 * 1.在其余任何事物发生以前,将分配给对象的存储空间初始化成二进制的零
 * 2.如前所述那样调用基类构造器。此时,调用被覆盖后的draw()方法(要在调用RoundGlyph构造器以前调用),
 *      因为步骤1的缘故,咱们此时会发现redius的值为0
 * 3.按照声明的顺序调用成员的初始化方法。
 */
public class PolyConstructors {
    /**
     * Glyph() before draw()
     * RoundGlyph.draw().redis = 0
     * Glyph() after draw()
     * RoundGlyph.RoundGlyph().redis = 5
     *
     */
    public static void main(String[] args) {
        new RoundGlyph(5);
    }
}
相关文章
相关标签/搜索