程序:
package basic;
public class polymorphism {
class Glyph {
Glyph() {
System.out.println("Glyph() before draw()");
draw();
System.out.println("Glyph() before draw()");
}
void draw(){
System.out.println("dddfee");
}
}
class RoundGlyph extends Glyph {
private int xx = 1;
RoundGlyph(int r) {
xx = r;
System.out.println("RoundGlyph.draw() " + xx);
}
void draw() {
System.out.println("RoundGlyph.draw() " + xx);
}
}
public static void main(String[] args) {
polymorphism ply = new polymorphism();
ply.new RoundGlyph(5);
}
}
运行结果:
Glyph() before draw()
RoundGlyph.draw() 0
Glyph() before draw()
RoundGlyph.draw() 5
package basic;
public class polymorphism {
class Glyph {
Glyph() {
System.out.println("Glyph() before draw()");
draw();
System.out.println("Glyph() before draw()");
}
void draw(){
System.out.println("dddfee");
}
}
class RoundGlyph extends Glyph {
private int xx = 1;
RoundGlyph(int r) {
xx = r;
System.out.println("RoundGlyph.draw() " + xx);
}
void draw() {
System.out.println("RoundGlyph.draw() " + xx);
}
}
public static void main(String[] args) {
polymorphism ply = new polymorphism();
ply.new RoundGlyph(5);
}
}
运行结果:
Glyph() before draw()
RoundGlyph.draw() 0
Glyph() before draw()
RoundGlyph.draw() 5