今天有个作业 用子类父类来写个求长方形体积的程序 由于才学 问题有点麻烦  大家帮我看看错哪些  谢了
class 长方形
{
double 长,宽,面积, 高;
长方形(double a double b double c)
{
a=长;
b=宽;
c= 高;
}
public double 面积()
{
面积= 长*宽;
return 面积;
}
}
class 长方体 extends长方形
{
double 体积;
double 体积()
{
体积=面积*高;
return 体积;
}
}
public class work18
{
public static void main(String args[])
{  
长方体 one;
one= new 长方体(10,5,7);System .out .println("体积"+one.体积());
}
}

解决方案 »

  1.   

    楼主,长方形那个类里没有“高”这个属性吧public class TestRectangle {
    public static void main(String[] args) {
    Cuboid cs=new Cuboid(2.0,2.0,3.0);
    System.out.println(cs.volume());
    }}
    //长方形
    class Rectangle{                                      
    private double length;        //长
    private double width;   //宽

    public Rectangle(double length,double width){
    this.length=length;
    this.width=width;

    }
    //计算面积
    public double area(){
    return width*length;
    }
    }
    //长方体
    class Cuboid extends Rectangle{
    private double height;                   //长方体的高

    public Cuboid(double length,double width,double height){
    super(length,width);                  //长和宽继承自父类
    this.height=height;
    }


    public double volume(){               
    return super.area()*height;           //体积等于面积乘以高,面积继承自父类
    }
    }get和set方法在这里没有加进去了,楼主完善一下吧...
      

  2.   

      这个就很完善了,子类中也不能直接访问父类的属性,用super,或get方法