酷代码 AI
菜单
服务商

Java 实现矩形与长方体类:计算底面积与体积的完整代码示例

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

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