51,408
社区成员
发帖
与我相关
我的任务
分享/**
* 矩形类,包含长宽,计算面积方法*/
public class Rectangle {
double length,width;
public double computeArea() {
return length*width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
/**
* 正方形类,面积方法重写*/
public class Square extends Rectangle{
@Override
public double computeArea() {
return length*length;
}
}
/**
* 长方体类, 有长宽高,计算体积方法*/
public class Cuboid extends Rectangle{
double hight;
public double getHight() {
return hight;
}
public void setHight(double hight) {
this.hight = hight;
}
public double computeVolume() {
return length*width*hight;
}
}
/**
* 测试类*/
public class Test {
public static void main(String[] args) {
Cuboid cuboid = new Cuboid();
cuboid.setLength(3.5);
cuboid.setWidth(2.7);
cuboid.setHight(2);
System.out.println("长方体底面积:"+cuboid.computeArea());
System.out.println("长方体体积:"+cuboid.computeVolume());
Square square = new Square();
square.setLength(3.5);
System.out.println("正方形面积:"+square.computeArea());
}
}