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"
不懂请大家赐教
...全文
26 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jyn01 2003-08-25
  • 打赏
  • 举报
回复
是不是ONE studio 4 update 1的设置不对啊?如果是是什么参数需要改动呢?
jyn01 2003-08-25
  • 打赏
  • 举报
回复
///:~
这个可是注解啊,del掉也没有用啊。
我del了过了,
java.lang.NoSuchMethodError: Triangle: method <init>()V not found
at Shapes.randShape(Shapes.java:44)
at Shapes.main(Shapes.java:51)
Exception in thread "main"
这样的错误,也就是还是一样啊。
仙鹤 2003-08-25
  • 打赏
  • 举报
回复
///:~
这个要删掉的
jyn01 2003-08-25
  • 打赏
  • 举报
回复
啊?我用的是xp ,ONE studio 4 update 1
fpwang 2003-08-25
  • 打赏
  • 举报
回复
我这里编译没有遇到任何错误
而且运行了

XP,J2sdk1.4.1_02

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧