矩形Rectangle是一个基类,包含两个数据成员长length和宽width,包含两个成员函数,一个是完成Rectangle数据成员初始化的构造函数,另一个是完成矩形面积计算的成员函数s( )。Box是以矩形rectangle为底面积的长方体。在主函数中定义Box的1个对象,完善派生类Box的成员定义,使得主函数调用该对象成员函数能够完成长方体体积的计算以及长方体表面积的计算以及输出
class Rectangle {
double width;
double height;
double depth;
public Rectangle() {
width = 1.0;
height = 1.0;
depth = 1.0;
}
public Rectangle(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
void getBox() {
System.out.println("box宽 :" + width);
System.out.println("box高 :" + height);
System.out.println("box深度:" + depth);
}
void volume() {
System.out.println("BOX体积:" + (width * height * depth));
}
void s() {
System.out.println("BOX面积 :" + 2
* (width * height + width * depth + height * depth));
}
}
public class test_three{
public static void main(String[] args) {
Rectangle box1 = new Rectangle();
System.out.println("第一个盒子:");
box1.getBox();
box1.volume();
box1.s();
System.out.println("第二个盒子");
Rectangle box2 = new Rectangle(5, 2, 5);
box2.getBox();
box2.volume();
box2.s();
}
}
