继承和多态 void报错!!!
[code]package haha_111;public class Point {
protected double x;
protected double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point() {
x = y = 0;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
public class Circle extends Point {
protected double r;
public Circle(double x, double y, double r) {
super(x, y);
this.r = r;
}
public Circle() {
super();
r = 0;
}
public double area() {
return Math.PI * r * r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
public class Cylinder extends Circle {
protected double h;
public Cylinder(double x, double y, double r, double h) {
super(x, y, r);
this.h = h;
}
public Cylinder() {
super();
h = 0;
}
public double volume() {
return area() * h;
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
}
public static void main(String[] args) {
Cylinder c1 = new Cylinder();
Cylinder c2 = new Cylinder(1, 2, 3, 4);
System.out.println("c1(" + c1.getX() + "," + c1.getY() + "):r=" +
c1.getR() + ",h=" + c1.getH() + ",volume=" +
c1.volume());
System.out.println("c2(" + c2.getX() + "," + c2.getY() + "):r=" +
c2.getR() + ",h=" + c2.getH() + ",volume=" +
c2.volume());
}[/code]
一般初学者在继承和多态时候都会遇见这个题目的,程序运行报错
错误信息:"Point.java": 'class' or 'interface' expected at line 88, column 15
请高手指点! 谢谢!
[[it] 本帖最后由 hanzhu3366 于 2008-5-21 00:41 编辑 [/it]]
class Point {
protected double x;
protected double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point() {
x = y = 0;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
class Circle extends Point {
protected double r;
public Circle(double x, double y, double r) {
super(x, y);
this.r = r;
}
public Circle() {
super();
r = 0;
}
public double area() {
return Math.PI * r * r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
class Cylinder extends Circle {
protected double h;
public Cylinder(double x, double y, double r, double h) {
super(x, y, r);
this.h = h;
}
public Cylinder() {
super();
h = 0;
}
public double volume() {
return area() * h;
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
}
class demo{
public static void main(String[] args) {
Cylinder c1 = new Cylinder();
Cylinder c2 = new Cylinder(1, 2, 3, 4);
System.out.println("c1(" + c1.getX() + "," + c1.getY() + "):r=" +
c1.getR() + ",h=" + c1.getH() + ",volume=" +
c1.volume());
System.out.println("c2(" + c2.getX() + "," + c2.getY() + "):r=" +
c2.getR() + ",h=" + c2.getH() + ",volume=" +
c2.volume());
}
}
回复 1# 的帖子
难受中ing...你不是帮别人写作业吧?这个貌似是群里提的问题...[tk15]
回复 4# 的帖子
这是一道作业题目! 但我对类的封装和多态了解不够透彻~~ 子类父类同名方法的应用。。因为java不像c++,都是引用。。当把子类对象赋给父类引用的时候都是子类覆盖父类方法。。抽象类是在一般基础上的扩展,它只能声明引用不能实例,但是由于是单继承概念的限制,所以提供接口。。回复 6# 的帖子
谢谢,我们现在只开了java,如果带着看c++,两门语言一起学,效率是不是强一些啊!?回复 7# 的帖子
效果肯定不好,等java入门后,稍微精通了,再去了解C++吧!不过等你精通java了,估计你是不会去看C++的![tk11]回复 6# 的帖子
貌似你还没有和hanzhu3366加为好友啊,或者他没有加你?我已经把你们俩都“收编”啦,两位也在私底下“活动活动”啊!
呵呵,[tk15] !
希望和二位结盟!
大意了
java.lang.NoSuchMethodError: mainException in thread "main"
编译通过,运行报错! 在网上查的几种可能;
1、文件名定义有问题
2、没有主函数
3、环境变量设置问题!
......
呵呵,刚刚找出来了,文件名定义有点问题!
[[it] 本帖最后由 hanzhu3366 于 2008-5-22 00:59 编辑 [/it]]
回复 10# 的帖子
你Java接触多久了 ?[tk12] 你应该看的出来哦
刚刚学,老师把课本上的例题一讲,其他的自己搞定! 呵呵!感觉还好!很想找一本习题集,不知道有什么好的电子图书?
回复 12# 的帖子
如果是起步不久的话,先找些题目练练手,听说有个《java百例》和《java书例》,都是源代码的!电子书我倒是收藏了很多,先送点基础的礼物给你。我就上传到我的空间,你去看看!
[tk10]
回复 12# 的帖子
另外,我已转入J2EE的学习,没有时间花在J2SE上了,有很多问题自己当初和现在也没有搞明白!还希望你能帮忙提醒下我! 我都在学。。但是感到时间不够。。现在迫切需要把算法和c#弄明白。。。算是基础吧。。不想了,一想就后悔我的时间啊[em04] 呵呵,大家加油!回复 15# 的帖子
算法?传说的中的燕子是个隐士呢,你去跟她过吧!C#,看来无法收编你啊!!!但是相信你以后会跟我好的!![tk16]
页:
[1]
