学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
轻松建立自己的群组,招兵买马   
 17 12
发新话题
打印

继承和多态 void报错!!!

继承和多态 void报错!!!

复制内容到剪贴板
代码:
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());

}
一般初学者在继承和多态时候都会遇见这个题目的,程序运行报错

错误信息:"Point.java": 'class' or 'interface' expected at line 88, column 15


请高手指点!  谢谢!

[ 本帖最后由 hanzhu3366 于 2008-5-21 00:41 编辑 ]

TOP

为什么有那么多public class啊?
I'm here, as always...

TOP

只有继承没有多态。。没有同名函数
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());

}
}
学习需要安静。。海盗要重新来过。。

TOP

回复 1# 的帖子

难受中ing...
你不是帮别人写作业吧?这个貌似是群里提的问题...
Java Lover QQ Group : 64666806

TOP

回复 4# 的帖子

这是一道作业题目! 但我对类的封装和多态了解不够透彻~~

TOP

子类父类同名方法的应用。。因为java不像c++,都是引用。。当把子类对象赋给父类引用的时候都是子类覆盖父类方法。。抽象类是在一般基础上的扩展,它只能声明引用不能实例,但是由于是单继承概念的限制,所以提供接口。。
学习需要安静。。海盗要重新来过。。

TOP

回复 6# 的帖子

谢谢,我们现在只开了java,如果带着看c++,两门语言一起学,效率是不是强一些啊!?

TOP

回复 7# 的帖子

效果肯定不好,等java入门后,稍微精通了,再去了解C++吧!不过等你精通java了,估计你是不会去看C++的!
Java Lover QQ Group : 64666806

TOP

回复 6# 的帖子

貌似你还没有和hanzhu3366加为好友啊,或者他没有加你?
我已经把你们俩都“收编”啦,两位也在私底下“活动活动”啊!
呵呵,
希望和二位结盟!
Java Lover QQ Group : 64666806

TOP

大意了

java.lang.NoSuchMethodError: main
Exception in thread "main"

编译通过,运行报错!  在网上查的几种可能;
1、文件名定义有问题
2、没有主函数
3、环境变量设置问题!

......

呵呵,刚刚找出来了,文件名定义有点问题!

[ 本帖最后由 hanzhu3366 于 2008-5-22 00:59 编辑 ]

TOP

 17 12
发新话题