|

楼主 |
发表于 2011-5-23 16:42
|
显示全部楼层
package exercises.Charpter8;
//: polymorphism/PolyConstructors.java
// Constructors and polymorphism
// don't produce what you might expect.
import static net.mindview.util.Print.*;
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);
}
}
class RectangularGlyph extends Glyph {
private int radius = 1;
RectangularGlyph(int r) {
radius = r;
print("RectangularGlyph.RectangularGlyph(), radius = " + radius);
}
void draw() {
print("RectangularGlyph.draw(), radius = " + radius);
}
}
public class Fifteen {
public static void main(String[] args) {
new RectangularGlyph(5);
}
} /* Output:
Glyph() before draw()
RoundGlyph.draw(), radius = 0
Glyph() after draw()
RoundGlyph.RoundGlyph(), radius = 5
*///:~
|
|