类内嵌套类是什么作用啊!!!
以下代码来自MSDN
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/csref/html/vclrfvirtualpg.htm
using
System;
class
TestClass
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class Dimensions
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public const double pi = Math.PI;
protected double x, y;
public Dimensions()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
public Dimensions (double x, double y)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.x = x;
this.y = y;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public virtual double Area()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return x*y;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public class Circle: Dimensions
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Circle(double r): base(r, 0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public override double Area()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return pi * x * x;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
class Sphere: Dimensions
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Sphere(double r): base(r, 0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public override double Area()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return 4 * pi * x * x;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
class Cylinder: Dimensions
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Cylinder(double r, double h): base(r, h)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public override double Area()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return 2*pi*x*x + 2*pi*x*y;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public static void Main()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
double r = 3.0, h = 5.0;
Dimensions c = new Circle(r);
Dimensions s = new Sphere(r);
Dimensions l = new Cylinder(r, h);
// Display results:
Console.WriteLine("Area of Circle = {0:F2}", c.Area());
Console.WriteLine("Area of Sphere = {0:F2}", s.Area());
Console.WriteLine("Area of Cylinder = {0:F2}", l.Area());
}
}