polyconstructors15.java
来自「JAVA编程思想第四版英文原版习题答案. pdf原版的」· Java 代码 · 共 48 行
JAVA
48 行
// polymorphism/PolyConstructors15.java
// TIJ4 Chapter Polymorphism, Exercise 15, page 303
// Add a RectangularGlyph to PolyConstructors.java and demonstrate the problem
// described in this section.
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 length = 2;
private int width = 4;
RectangularGlyph(int l, int w) {
length = l;
width = w;
print("RectangularGlyph.RectangularGlyph(), length = "
+ length + ", width = " + width);
}
void draw() {
print("RectangularGlyph.draw(), length = " + length
+ ", width = " + width);
}
}
public class PolyConstructors15 {
public static void main(String[] args) {
new RoundGlyph(5);
new RectangularGlyph(3, 6);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?