酷代码 AI
菜单
服务商

Java 实现矩形与长方体类,轻松计算底面积和体积

```java class Rectangle { double length; double width; double calculateArea() { return length * width; } } class Cuboid extends Rectangle { double height; double calculateVolume() { return calculateArea() * height; } } public class Test { public static void main(String[] args) { double length = 5.4; double width = 3.2; double height = 2.5; Cuboid cuboid = new Cuboid(); cuboid.length = length; cuboid.width = width; cuboid.height = height; System.out.println("这是体积计算:" + cuboid.calculateVolume()); System.out.println("这是底面积计算:" + cuboid.calculateArea()); System.out.println("另一种实例化方式:"); Rectangle rectangle = new Rectangle(); rectangle.length = length; rectangle.width = width; Cuboid cuboid2 = new Cuboid(); cuboid2.length = length; cuboid2.width = width; cuboid2.height = height; System.out.println("这是底面积计算:" + rectangle.calculateArea()); System.out.println("这是体积计算:" + cuboid2.calculateVolume()); } } ``` ######[AI写代码神器 | 308点数解答 | 2024-10-20 10:34:27]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]