设为首页 加入收藏

TOP

9.9.4 定义接口类(2)
2013-10-07 12:42:02 来源: 作者: 【 】 浏览:66
Tags:9.9.4 定义 接口

9.9.4  定义接口类(2)

试一试:实现接口类

创建CLR控制台项目Ex9_15,并添加内容如前所示的IContainer.h和Box.h头文件。我们还应该将Ex9_14中Stack.h和GlassBox.h头文件的副本添加到该项目中。最后,将Ex9_15.cpp的内容修改成下面这样:

  1. // Ex9_15.cpp : main project file.  
  2. // Implementing an interface class  
  3. #include "stdafx.h"  
  4. #include "Box.h"    // For Box and IContainer  
  5. #include "GlassBox.h" // For GlassBox (and Box and IContainer)  
  6. #include "Stack.h"          // For the stack 
    class with nested struct Item  
  7.  
  8. using namespace System;  
  9.  
  10. int main(array<System::String ^> ^args)  
  11. {  
  12. array<IContainer^>containers = { gcnew Box(2.0, 3.0, 4.0),  
  13.                                          
    gcnew GlassBox(2.0, 3.0, 4.0),  
  14.                                             
    gcnew Box(4.0, 5.0, 6.0),  
  15.                                            
    gcnew GlassBox(4.0, 5.0, 6.0)  
  16.                                           };  
  17. Console::WriteLine(L"The array of containers 
    have the following volumes:");  
  18. for each(IContainer^ container in containers)  
  19. container->ShowVolume();            // Output the volume of a box  
  20.  
  21. Console::WriteLine(L"\nNow pushing the containers on the stack...");  
  22.  
  23. Stack^ stack = gcnew Stack;             // Create the stack  
  24. for each(IContainer^ container in containers)  
  25. stack->Push(container);  
  26.  
  27. Console::WriteLine(  
  28.        L"Popping the containers off the stack 
    presents them in reverse order:");  
  29. Object^ item;  
  30. while((item = stack->Pop()) != nullptr)  
  31. safe_cast<IContainer^>(item)->ShowVolume();  
  32.  
  33. Console::WriteLine();  
  34. return 0;  
  35. }  

该示例产生下面的输出:

  1. The array of containers have the following volumes:  
  2. CBox usable volume is 24  
  3. CBox usable volume is 20.4  
  4. CBox usable volume is 120  
  5. CBox usable volume is 102  
  6.  
  7. Now pushing the containers on the stack...  
  8. Popping the containers off the stack presents them in reverse order:  
  9. CBox usable volume is 102  
  10. CBox usable volume is 120  
  11. CBox usable volume is 20.4  
  12. CBox usable volume is 24 

示例说明

我们首先创建了一个元素类型为IContainer^的数组,并将数组元素初始化为Box和GlassBox对象的地址:

  1. array<IContainer^>containers = { gcnew Box(2.0, 3.0, 4.0),  
  2.                                           gcnew GlassBox(2.0, 3.0, 4.0),  
  3.                                           gcnew Box(4.0, 5.0, 6.0),  
  4.                                           gcnew GlassBox(4.0, 5.0, 6.0)  
  5.                                         }; 

Box和GlassBox类实现了IContainer接口,因此我们可以在类型为IContainer句柄的变量中存储这两个类的对象地址。这样做的好处是能够多态地调用IContainer接口类的函数成员。

下面的for each循环输出Box和GlassBox对象的体积:

  1. for each(IContainer^ container in containers)  
  2. container->ShowVolume();            // Output the volume of a box 

该循环体说明多态性在起作用,从输出可以看出,被调用的ShowVolume()函数是container引用的对象所属的特定类型的成员。

我们按照上一个示例的方法,将containers数组的元素压入堆栈。将这些元素弹出堆栈的操作同样类似于上一个示例:

  1. Object^ item;  
  2. while((item = stack->Pop()) != nullptr)  
  3. safe_cast<IContainer^>(item)->ShowVolume(); 

循环体表明,我们可以用与强制转换引用类类型时完全相同的方式,使用safe_cast强制转换指向接口类型的句柄。之后,就能使用该句柄多态地调用ShowVolume()函数。

使用接口类不仅可以定义一组表示标准类接口的函数,而且是一种强大的、支持在程序中应用多态性的机制。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇9.9.1 装箱与拆箱 下一篇9.9.4 定义接口类(1)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: