thinking in java里面的程序编译出错看不出为什么
jyn01 2003-08-25 11:01:23 我在看thinking in java的时候做其练习题,用例题的时候便以出错,可是程序好像没有错误,请大家看看第7章关于多态的
//: c07:Shapes.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Polymorphism in Java.
class Shape {
void draw() {}
void erase() {}
}
class Circle extends Shape {
void draw() {
System.out.println("Circle.draw()");
}
void erase() {
System.out.println("Circle.erase()");
}
}
class Square extends Shape {
void draw() {
System.out.println("Square.draw()");
}
void erase() {
System.out.println("Square.erase()");
}
}
class Triangle extends Shape {
void draw() {
System.out.println("Triangle.draw()");
}
void erase() {
System.out.println("Triangle.erase()");
}
}
public class Shapes {
public static Shape randShape() {
switch((int)(Math.random() * 3)) {
default:
case 0: return new Circle();
case 1: return new Square();
case 2: return new Triangle();
}
}
public static void main(String[] args) {
Shape[] s = new Shape[9];
// Fill up the array with shapes:
for(int i = 0; i < s.length; i++)
s[i] = randShape();
// Make polymorphic method calls:
for(int i = 0; i < s.length; i++)
s[i].draw();
}
} ///:~
编译的结果:
java.lang.NoSuchMethodError: Shape: method <init>()V not found
at Square.<init>(Shapes.java:20)
at Shapes.randShape(Shapes.java:43)
at Shapes.main(Shapes.java:51)
Exception in thread "main"
不懂请大家赐教