类内嵌套类是什么作用啊

博客展示了类内嵌套类的使用,在TestClass中定义了Dimensions类及继承它的Circle、Sphere、Cylinder类,各子类重写了Area方法以计算不同图形面积,最后在Main方法中创建对象并输出结果,体现了类内嵌套类在面向对象编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类内嵌套类是什么作用啊!!!
以下代码来自MSDN
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/csref/html/vclrfvirtualpg.htm

None.gifusing System;
None.gif
class TestClass 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
public class Dimensions 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
public const double pi = Math.PI;
InBlock.gif      
protected double x, y;
InBlock.gif      
public Dimensions() 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockEnd.gif      }

InBlock.gif      
public Dimensions (double x, double y) 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         
this.x = x;
InBlock.gif         
this.y = y;
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
public virtual double Area() 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         
return x*y;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public class Circle: Dimensions 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
public Circle(double r): base(r, 0
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
public override double Area() 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif
InBlock.gif         
return pi * x * x; 
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
class Sphere: Dimensions 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
public Sphere(double r): base(r, 0
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
public override double Area()
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         
return 4 * pi * x * x; 
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
class Cylinder: Dimensions 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
public Cylinder(double r, double h): base(r, h) 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
public override double Area() 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         
return 2*pi*x*+ 2*pi*x*y; 
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
public static void Main()  
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
double r = 3.0, h = 5.0;
InBlock.gif      Dimensions c 
= new Circle(r);
InBlock.gif      Dimensions s 
= new Sphere(r);
InBlock.gif      Dimensions l 
= new Cylinder(r, h);
InBlock.gif      
// Display results:
InBlock.gif
      Console.WriteLine("Area of Circle   = {0:F2}", c.Area());
InBlock.gif      Console.WriteLine(
"Area of Sphere   = {0:F2}", s.Area());
InBlock.gif      Console.WriteLine(
"Area of Cylinder = {0:F2}", l.Area());
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/sbj0707/archive/2005/08/19/218177.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值