定义一个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);
}
}
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;
}
}
}
就在Cute类里面写一个Rectangle类,没什么特别的,只要是Cute类使用这个内部类的话用法和平常没什么区别
/**
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;
}
}
}