abstract class Glyph {
abstract void draw();
Glyph() {
System.out.println("Glyph() before draw()");
draw();
System.out.println("Glyph() after draw()");
}
}
class RoundGlyph01 extends Glyph {
int radius=0;
String s;
RoundGlyph01(int r,String s) {
radius=r;
this.s=s;
System.out.println("RoundGlyph01.RoundGlyph(),radius="+radius+" "+s);
}
void draw() {
System.out.println("RoundGlyph01.draw(),radius="+radius+" "+s);
}
}
class RoundGlyph02 extends Glyph {
int radius=0;
String s;
RoundGlyph02(int r,String s) {
radius=r;
this.s=s;
System.out.println("RoundGlyph02.RoundGlyph(),radius="+radius+" "+s);
}
void draw() {
System.out.println("RoundGlyph02.draw(),radius="+radius+" "+s);
}
}
class RoundGlyph03 extends RoundGlyph01 {
int radius=0;
String s;
RoundGlyph03(int r,String s) {
radius=r;
this.s=s;
System.out.println("RoundGlyph03.RoundGlyph(),radius="+radius+" "+s);
}
void draw() {
System.out.println("RoundGlyph03.draw(),radius="+radius+" "+s);
}
}
public class PolyConstructors {
public static void main(String[] args) {
System.out.println("Creating object RoundDlyph01...");
new RoundGlyph01(5,"String");
System.out.println("**********************************************");
System.out.println("Creating object RoundDlyph02...");
new RoundGlyph02(5,"String");
System.out.println("**********************************************");
System.out.println("Creating object RoundDlyph03...");
new RoundGlyph03(5,"String");
System.out.println("**********************************************");
}
}
编译结果:
PolyConstructors.java:40: cannot resolve symbol
symbol : constructor RoundGlyph01 ()
location: class RoundGlyph01
RoundGlyph03(int r,String s) {
^
1 error
经过测试,如果我把RoundGlyph03 继承Glyph而非RoundGlyph01,一切正常,那么为什么继承RoundGlyph01就会出现如上错误呢?