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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
'------------------------------------------------------------------------------------------------------------------------------------------ '本示例旨在向您展示如何在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。 '------------------------------------------------------------------------------------------------------------------------------------------
在Visual Studio 2003下开发网站的都知道ASP.NET 1.1网站生成后都是一个唯一命名的程序集(项目名称.DLL),但是这种情况在Visual Studio 2005的网站开发不见了。Visual Studio 2005给我们提供了“发布网站”这样方便的功能,不用在像以前一样COPY的部署,但是采用其“发布网站”的功能,会在站点的BIN目录产生App_Code.compiled,App_Code.dll,App_Web_xxxxxx.dll,项目名称.dll等等这样的程序集,如果不采用预编译的方式那么产生的文件会更加的多,因为很多都是随机命名也没有什么规律可言;这样会给那些想COPY更新的人带来不便,因为原来的文件不能被覆盖,需要先删除再COPY;有很多时候可能就是更新了后台代码,直接生成项目或是WEB的程序集,像以前一样直接覆盖DLL的方式也很简单。 传统的生成或是发布网站的方式没有任何问题,当使用Web Deployment Projects工具生成唯一程序集时出现了错误信息“Aspnet_merge.exe Exited with Code 1”(Aspnet_merge.exe 退出,错误代码1),这样的信息根本无法让我们办断到底是哪里出了问题。好,下面依次打开VS 2005的工具-选项-项目和解决方案-生成并运行,设置“MSBuild 项目生成输出详细信息”,选择“详细”。再次生成Web Deployment Projects项目,注意“输出”框内的信息现在变得非常丰富了,最后导致无法编译的问题终于出来了“An error occurred when merging assemblies: ILMerge.Merge: ERROR!!: Duplicate type ‘_Default’ found in assembly 'App_Web_k5hhsnh0'”,它的意思是说在程序集发现了相同的类型_Default,经过我检查确实在我们的WEB项目存在了两个_Default类(在不同的目录),这样的问题通过一般的生成完成和发布网站肯定都是检测不出来的,运行也是正常的。VS 2005的WEB项目默认的把命名空间给去掉了,可以手动增加一个命名空间,cs代码加入namespace的语句块,另外别忘了在aspx文件的<@page>的Inherits写入对应的[namespace].[类名];经过这样的操作再次编译成一个程序集就没有问题了。或者直接把aspx页面的Inherits改成“路径_文件名”,cs文件的类名也改成同样的名称;可是如果一个网站有很多的目录,很多的文件,而且又有多个客户版本,那修改起来就非常的麻烦,为此特意开发了该工具,大大提高了工作效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值