```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()); } } ``` [2024-10-20 10:34:27 | AI写代码神器 | 308点数解答]