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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值