- 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, radius = " + radius);
- }
-
- void draw(){
- System.out.println("RoundGlyph.draw(), radius = " + radius);
- }
- }
- public class PolyConstructor {
- public static void main(String[] args) {
- new RoundGlyph(5);
- }
- }
复制代码
运行结果:
Glyph before draw
RoundGlyph.draw(), radius = 0
Glyph after draw
RoundGlyph, radius = 5
|
|