MS Access中VBA代码的继承 [英] Inheritance for VBA code in MS Access

查看:103
本文介绍了MS Access中VBA代码的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始在Access中学习VBA.我已经读过该语言没有继承性. 然后,我阅读了一个看起来确实具有继承性的示例代码:

I've started to learn VBA in Access. I have read that the language has no inheritance. And then I read an example code which seems like it actually has inheritance:

Dim ctrl As Control

...

If TypeOf ctrl Is TextBox Then ...
If TypeOf ctrl Is ListBox Then ...

在我看来,就像TextBox,ListBox都是从Control继承的.有人可以解释一下吗?

It seems to me as the TextBox, ListBox were inherited from the Control. Could somebody explain this?

推荐答案

否.它们不是从Control类派生的.它们实现Control的定义/方法和属性签名. TypeOfIs运算符的工作方式是检查类的实例是否实现3类(下面列出)之一.

No. They are not derived from the Control class. They implement Control's definition/ methods and properties signatures. The way TypeOf and Is operators work it's they check whether the instance of a class Implements one of 3 categories (listed below).

打开工作簿

转到VBE并添加

一个类模块并将其命名为:MyClass

在代码视图中仅添加Implements MyInterface

一个类模块并将其命名为:MyInterface

在代码视图中-不执行任何操作/保留为空

a 模块,然后复制粘贴以下代码并运行

a module and copy paste the below code and run it

Sub Main()

    Dim cls As MyClass
    Set cls = New MyClass

    Debug.Print TypeOf cls Is MyClass
    Debug.Print TypeOf cls Is MyInterface

End Sub

结果可能令人惊讶

True
True

cls变量有两种类型-MyClassMyInterface

cls不会从MyInterface继承任何东西,而是继承定义.当使用TypeOfIs时,它实际上显示为true,因为MyClass实现了MyInterface.不是因为它是从MyInterface类派生的,而是因为它实现了它.

as you can see cls doesn't inherit nothing from MyInterface but definition. When using TypeOf and Is it actually shows true because MyClass implements MyInterface. Not because it's derived from the MyInterface class but because it implements it.

现在,假设

result = TypeOf objectexpression Is typename

TypeOf运算符确定变量的运行时类型是否与 typename 兼容.兼容性取决于 typename 的类型类别.分为三类

The TypeOf operator determines whether the run-time type of variable is compatible with typename. The compatibility depends on the type category of typename. There are three categories

  • objectexpression 的类型为 typename 或继承自 typename

  • Class objectexpression is of type typename or inherits from typename

结构 objectexpression 的类型为 typename

接口 对象表达式实现 typename 继承自实现类的 类型名称

Interface objectexpression implements typename or inherits from a class that implements typename

具体尝试了解第三个类别-接口.

Specifically try to understand the 3rd category - Interface.

我认为在这一点上您应该真正理解为什么TypeOf varName Is varTypeTextBoxListBox显示True ...

I think at this point you should really understand why TypeOf varName Is varType shows True for the TextBox and ListBox...

进行VBA继承时,只能使用实施关键字来 实现定义类.也就是说,要实现的类是 等效于C ++/C#的抽象类:仅具有属性/方法 定义.

When you do VBA inheritance, you can only use Implements keyword to implements a class definition. That is, the class to be implemented is equivalent to C++/C#'s abstract class: only having property/method definition.

通常,您的示例不是类多态的一种形式.当您实际上将一个实例派生到另一个实例时,就会发生类多态.事实并非如此.即使TextBoxListBox都是Control类型,它们实际上并不是从Control类派生的.注意:它们也可能是另一个集合的成员-在对象层次结构类型中,它们TypeOf较高(表单,以及Component和IComponent,因为Forms实现了该实现).

Generally, your example isn't a form of a class polymorphism. Class polymorphism occurs when you are actually deriving an instance of one to class to another. This isn't the case. Even though TextBox and ListBox are both of a Control type they aren't actually derived from Control class. Note: they may as well be members of another collection - they would be TypeOf the higher in the object hierarchy type ( forms, also Component and IComponent becuase Forms implements that).

这篇关于MS Access中VBA代码的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆