java实验题 内部类的定义与使用

5个回答

`````` 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);
}
}
``````

using System; class UseRectangle { static void Main() { double lenth = 23.3; double wide = 19.4; double high = 30.5; Console.Write("The Rectangle lenth={0} wide={1} high={2} of cube=", lenth, wide, high); Console.WriteLine("{0}", cube(lenth, wide, high)); } static double cube(double lenth, double wide, double high) { return lenth * wide * high; } }

`````` public class Cube {
private double height;
public double tiji(double area){
return area*height;
}
class Rectangle{
public double height;
public double width;
public double area(double length,double width){
return length*width;
}
}
}

``````

/**

• 外部类Cube */

public class Cube {
private double height;

``````    /**
* 计算体积的方法
* @param area 面积
* @return 当前物体的体积
*/

public double volume(double area){
return area * height;
}

/**
* 内部类Rectangle
* 包含属性:width、height
* 包含方法:volume(double length,double width)
*/

class Rectangle{
public double width;
public double height;

/**
* 计算底面积的方法
* @param length
* @param width
* @return 当前物体的底面积
*/

public double volume(double width ,double length){
return width * length;
}
}
``````

}