Java类型转换问题,我是新手,请哥哥们解释下为什么错了,

[复制链接]
查看11 | 回复1 | 2011-6-20 21:15:35 | 显示全部楼层 |阅读模式
/**
* 请指出如下代码中存在的错误,并解释原因。注释错误语句后的输出结果是什么,并解释原因
* @author Administrator
*
*/
abstract class Shape {
        public abstract double getArea();
}
class Square extends Shape{
        private double height=0;//长方形的边长
        public Square(double height){
                this.height=height;
        }
        public double getArea() {
                return (this.height*this.height);
        }
       
}
class Circle extends Shape{
        private double r=0;//员的半径
        private final staticdouble PI=3.14;//圆周率
        public Circle (double r){
                this.r=r;
        }
        public double getArea() {
               
                return PI*r*r;
        }
       
}
class Test{
        public static void main(String[] args) {
                Shape square =new Square(3);
                Shape circle=new Circle(2);
                System.out.println(square.getArea());
                System.out.println(circle.getArea());
                Square sq=(Square)circle();
                System.out.println(sq.getArea());
        }
}

回复

使用道具 举报

千问 | 2011-6-20 21:15:35 | 显示全部楼层
public static void main(String[] args) {Shape square =new Square(3);Shape circle=new Circle(2);System.out.println(square.getArea());System.out.println(circle.getArea());Square sq=(Square)square; System.out.println(sq.getArea());}主函数修改如上,首先Square sq=(Square)circle();这个转换,是要把一个函数转化为一个对象实例,这是不可能实现的。然后将父类的对象转化为子...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行