用java制作实现一个长方体的盒子类用构造方法实例化一个具体的盒子算出这个盒子的体积和表面积
//盒子类 public class Box { double length; double width; double height;
//构造方法
public Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
//计算体积
public double getVolume() {
return length * width * height;
}
//计算表面积
public double getSurfaceArea() {
return 2 * (length * width + width * height + height * length);
}
}
//测试类 public class TestBox { public static void main(String[] args) { Box box = new Box(5.0, 6.0, 7.0); System.out.println("盒子的体积为:" + box.getVolume()); System.out.println("盒子的表面积为:" + box.getSurfaceArea()); } }
原文地址: http://www.cveoy.top/t/topic/KVY 著作权归作者所有。请勿转载和采集!