定義一個Cube類,其中包含一個變量height,一個方法:計算立方體的體積(帶形參,形參為面積)。並在其內部定義一個Rectangle類,其中包含兩個變量width和height,一個方法:計算底面積。
class Cube {
private double height;
Cube() {}
Cube(double height) {
this.height = height;
}
public double volume(double area) {
return area * height;
}
public class Rectangle {
private double width;
private double length;
Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
public double area() {
return length * width;
}
}
}
public class Test {
public static void main(String[] args) {
Cube.Rectangle S = new Cube().new Rectangle(10.0, 10.0);
double s = S.area();
Cube V = new Cube(20.0);
double v= V.volume(s);
System.out.println("立方體的體積 = " + v);
}
}