VS2005中提供了一个可视的设计图面(称为“类关系图”),可用于处理项目中的类和其他类型。类关系图中显示的元素对应于代码中的元素,因此类设计器为您提供了代码的可视化形式。

下面通过例子来学习一下类设计器的使用方法。

 
  
  1. public class Shape  
  2. {  
  3.     public virtual void Draw()  
  4.     { ;} //虚方法用于图形绘制  
  5. }  
  6. public class Rectangle : Shape //定义矩形类  
  7. {  
  8.     protected int a;  
  9.     protected int b; //矩形的边长  
  10.     public Rectangle(int va, int vb)  
  11.     {  
  12.         a = va;  
  13.         b = vb;  
  14.     }  
  15.     public override void Draw() //重载虚方法在屏幕上绘制矩形  
  16.     {  
  17.         Console.WriteLine("Rectangle:");  
  18.         Console.WriteLine("* * * * *");  
  19.         Console.WriteLine("*       *");  
  20.         Console.WriteLine("*       *");  
  21.         Console.WriteLine("* * * * *");  
  22.     }  
  23. }  
  24. public class Square : Rectangle //定义正方形类  
  25. {  
  26.     public Square(int va)  
  27.         : base(va, va)  
  28.     { ;}  
  29.     public override void Draw() //重载绘制正方形  
  30.     {  
  31.         Console.WriteLine("Square");  
  32.         Console.WriteLine("* * * * *");  
  33.         Console.WriteLine("*       *");  
  34.         Console.WriteLine("*       *");  
  35.         Console.WriteLine("*       *");  
  36.         Console.WriteLine("* * * * *");  
  37.     }  
  38. }  
  39. //定义普通三角形作为其它三角形的基类  
  40. public class Triangle : Shape  
  41. {  
  42.     protected int a;  
  43.     protected int b;  
  44.     protected int c;  
  45.     public Triangle(int va, int vb, int vc)  
  46.     {  
  47.         a = va;  
  48.         b = vb;  
  49.         c = vc;  
  50.     }  
  51. }  
  52. //定义直角三角形  
  53. public class RectTriangle : Triangle  
  54. {  
  55.     new protected int a;  
  56.     new protected int b;  
  57.     public RectTriangle(int va, int vb)  
  58.         : base(va, vb, (int)(Math.Sqrt(va * va + vb * vb)))  
  59.     {  
  60.         a = va;  
  61.         b = vb;  
  62.     }  
  63.     public override void Draw()  
  64.     {  
  65.         Console.WriteLine("RectTriangle");  
  66.         Console.WriteLine("*");  
  67.         Console.WriteLine("* *");  
  68.         Console.WriteLine("* * *");  
  69.         Console.WriteLine("* * * *");  
  70.     }  
  71. }  
  72. //定义等腰直角三角形  
  73. public class RectEqualTriangle : RectTriangle  
  74. {  
  75.     new protected int a;  
  76.     public RectEqualTriangle(int va)  
  77.         : base(va, va)  
  78.     {  
  79.         a = va;  
  80.     }  
  81.     public override void Draw()  
  82.     {  
  83.         Console.WriteLine("RectEqualTriangle");  
  84.         Console.WriteLine("*");  
  85.         Console.WriteLine("* *");  
  86.         Console.WriteLine("* * *");  
  87.         Console.WriteLine("* * * *");  
  88.         Console.WriteLine("* * * * *");  
  89.     }  

在上面的代码中,有一个基类Shape,基类Shape派生矩形类Rectangle,矩形类Rectangle派生正方形类Square,同样,基类Shape派生三角形类Triangle,三角形类Triangle派生直角三角线RectTriangle,直角三角线RectTriangle又派生等腰直角三角形RectEqualTriangle。

 在VS2005中,右击上面的类所在的项目,在弹出的快捷菜单中选择“添加”-->“新建项”,在弹出的“添加新项”对话框中,选择“类关系图”,然后单击“添加”按钮,如下图所示:

类关系图是以“cd”扩展名结尾的。在ClassDiagram1.cd中单击“类视图”,就会出现“类视图”窗口,如下图所示:

将“类视图”窗口中相关的类都拖放到ClassDiagram1.cd中,如下图所示:

这样的类关系图看起来可能有些混乱,在页面上右击鼠标,在弹出的快捷菜单中选择“布局关系图”,如下图所示:

经过重新布局后的类关系图就一目了然了,如下图所示:

 

除此之外,还可以在类关系图上执行其他工作,例如,为类关系图添加注释信息。在类关系图页面右击鼠标,在弹出的快捷菜单中选择“添加”-->“注释”,如下图所示:

可以为该类关系图添加一些说明信息,如下图所示:

 

 

还可以将类关系图复制到其他地方,比如Word中,方法是:全选并右击类关系图,选择“复制图像”就可以了。