欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

构造器内部的多态方法的行为[Java编程思想]

程序员文章站 2022-06-07 21:28:29
...

 

class Glyph {
	void draw() {	print("Glyph.draw()");	}
	Glyph() {
		print("Glyph() before draw()");
		draw();
		print("Glyph() after draw()");
	}
}

class RoundGlyph extends Glyph {
	private int radius = 1;
	RoundGlyph(int r) {
		radius = r;
		print("RoundGlyph.RoundGlyph(), radius = " + radius);
	}

	void draw() {
		print("RoundGlyph.draw(), radius = " + radius);
	}
}

public class PolyConstructors {
	public static void main(String[] args) {
		new RoundGlyph(5);
	}
}

//output:
//Glyph() before draw()
//RoundGlyph.RoundGlyph(), radius = 0
//Glyph() after draw()
//RoundGlyph.draw(), radius = 5
相关标签: java