VBA中的“继承”和多态

Technorati 标签: vba, 继承, 多态, 接口类, 实现类

在Patricia Cardoza等著的《ACCESS 2003 VBA Programmer's Reference》一书中,有如下关于VBA继承的叙述:

“In a nutshell, inheritance is the ability to create new classes from existing ones. A derived class, or subclass, inherits the properties and methods of the class that instantiated it (called the base class, or superclass), and may add new properties and methods. New methods can be defined with the same name as those in the superclass, in which case they override the original one.

There are two types of inheritance: interface and implementation inheritance. Interface inheritance has been available to Access since VBA 6.0 introduced the Implements keyword. Implementation inheritance is now available in Visual Basic .NET through the Inherits keyword, but unfortunately not in Access 2003.

The essential difference between the two forms of inheritance is that interface inheritance specifies only the interface. It doesn’t actually provide any corresponding implementation code. For example, suppose we have a Bike object that wants to ask the Wheel object for its part number. The Wheel object wants to borrow the functionality from its superclass, Parts. The Bike object might implement the following functionality:

The implementation of this behavior is in Parts' PartNo() method. Because VBA 6.0 doesn’t support implementation inheritance, you would need to put some code into the Wheel class. 

VBA 6.0 allows the interface, in this case Part, to implement the actual behavior. Wheel retains an instance of Part (a behavior called containment), and then asks that reference to carry out some action for it (called delegation). This isn’t true interface inheritance because it allows you to add code to Wheel to provide the actual behavior, but it’s close enough.”

在后续的章节还详细解释了implements语句的用法,并觉了如下例子说明这种多态的用法。

“We like the example of dogs, where we implement different breeds, like German Shepherds, Poodles, and Huskies. Each breed has its own physical attributes and behavioral patterns, such that they each merit their own class (at least for this example). All of them share common traits: they all belong to the canine family, all have height, weight, color, and so on. All bark, eat, and drop little presents on the lawn. If we put all the common traits into a single interface (IDog), we have a generic way of dealing with all breeds at once. For example, we can feed the entire kennel like so:

Although all dogs eat, they all perform the act of eating in very different (and sometimes bizarre) ways. Therefore, the code to implement each dog’s unique eating habits must be in that dog’s class, and will certainly differ from dog to dog.”

小结和感觉如下:

  1. VBA中只支持interface inheritance。这种继承与其说是继承,不如说是多态。
  2. 其继承的主要方式是通过implements语句让实例类自动继承接口类中声明的变量和过程。
  3. 接口类一般命名中以I打头,如IDog。
  4. 接口类中的函数貌似不能有实现code,是纯粹的虚函数,专为其子类继承接口用。
  5. 在我看来,这种所谓的“继承”唯一的用处就是上面最后那段代码。

参考文献

Patricia Cardoza, Teresa Hennig, Graham Seach, Armen Stein, "Access 2003 VBA Programmer’s Reference", Published by Wiley Publishing, Inc., Indianapolis, Indiana, 2004

'------------------------------------------------------------------------------------------------------------------------------------------ '本示例旨在向您展示如何在VB6下实现真正的继承,如何使用DyCOMHelper创建类,并使用,它将带您进入VB6背后的COM世界,看看我们的类是如何运行的。 'DyCOMHelper创建的对象的特点: '1、可以像VB6下定义的对象一样去使用,虽然不是VB6机制创建,但是可以被VB6当作对象来识别使用。 '2、支持后期绑定,支持错误处理,支持多接口等常规应用。 '3、支持真正意义上的继承,本示例将说明这一点。 '4、所创建的对象是轻量对象,实例占用的起步空间更小,约VB类的1/5。 '5、函数调用速度高出20% 左右 '6、实例创建的速度,最大可以提高近10倍 '7、释放速度,最大可以提高近100倍。(以上速度来自于极限测试数据) '8、创建模式比VB6更丰富,VB6的类实例,是在堆上进行创建的,效率很低,DyComHelper可以选择堆、堆栈是进行创建,也可以从某个结构上创建,也可以用内置的定长管理器进行创建。 '9、从类实例外部,访问内部数据时,支持真正意义的指针式访问。 '10、使用DyCOMHelper创建类,将让您的系统支持成千上万个类,而不会担心效率问题。 ' '读这个示例您可能会问的问题: ' 1、类型库,怎么来的?目前是我使用工具手动创建。未来,我将推出VB6插件,在VB6下直接书写代码生成类型库并自动引用。 ' '使用前,请先执行下列顺序: ' 1、引用 Types目录下 DyCOMHelperType.tlb,TestInheritLib.tlb。 ' 2、按F8开始逐步断点执行,看看它是如何工作的。 '示例中,有三个类,Animal、Wolf、Demiwolf,分别是动物、狼、狼狗,狼狗继承自狼,狼继承自动物,动物派生自IDispatch。 '------------------------------------------------------------------------------------------------------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值