vba 可用的 OOP 概念列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3140037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List of Available OOP Concepts
提问by amr osama
I am building some material for OOP (object oriented programming) in VBA. Can anybody list me the OOP concepts which are available in VBA?
我正在为 VBA 中的 OOP(面向对象编程)构建一些材料。有人可以列出 VBA 中可用的 OOP 概念吗?
For example, from my reading I discovered that:
例如,从我的阅读中我发现:
- Inheritance is not available in VBA.
- The encapsulation concept is there, as you can use access modifier "private" and build a public property.
- 继承在 VBA 中不可用。
- 封装概念就在那里,因为您可以使用访问修饰符“private”并构建公共属性。
采纳答案by Ben McCormack
Here are some observations I've made while working with OOP concepts in VBA:
以下是我在 VBA 中使用 OOP 概念时所做的一些观察:
- You cannot overload methods in VBA. However, you do have optional parameters at your disposable, for better or for worse.
- You have one parameterless
Class_Initialize
method that is called when the object is instantiated, but it cannot be overloaded to handle parameters. If you want to force your class to not be "fully functional" without having certain properties set, you'll have to write your own way to do so. - The VB6 and VBA editing environment forces you to build "class files" and keep each class in a separate file, which are distinct from modules.
- Classes and Modules can both have public and private fields. A public field in a module is essentially a global variable.
- Modules are functionally similar to static classes in C#. Public code can be called from the modules anywhere within your application.
- 您不能在 VBA 中重载方法。但是,无论好坏,您都可以随意使用可选参数。
- 您有一个
Class_Initialize
在实例化对象时调用的无参数方法,但不能重载它来处理参数。如果你想强制你的类在没有设置某些属性的情况下不能“完全发挥作用”,你必须编写自己的方法来做到这一点。 - VB6 和 VBA 编辑环境强制您构建“类文件”并将每个类保存在一个单独的文件中,这些文件与模块不同。
- 类和模块都可以有公共和私有字段。模块中的公共字段本质上是一个全局变量。
- 模块在功能上类似于 C# 中的静态类。可以从应用程序中任何位置的模块调用公共代码。
The VB6/VBA paradigm envisions classes as a way to encapsulate an object's functionality and properties. In this sense, VB6/VBA's objects exist just like any other basic OOP environment and their use and design should be encouraged where appropriate.
VB6/VBA 范式将类设想为封装对象功能和属性的一种方式。从这个意义上说,VB6/VBA 的对象就像任何其他基本的 OOP 环境一样存在,应该在适当的地方鼓励它们的使用和设计。
However, the lack of several key OOP features cause VB6/VBA to fall short in being able to thoroughly implement a complete OOP design pattern.
然而,由于缺乏几个关键的 OOP 特性,VB6/VBA 无法彻底实现完整的 OOP 设计模式。
回答by pbanfi
VBA supports some OO concepts, and not others.
VBA 支持某些 OO 概念,而不支持其他概念。
with VBA you can create your own classes, and you can create objects from those classes. However, VBA does NOT support Inheritance, and does not truly support 'polymorphism' in the classical meaning of the term as used in OO languages such as C++, or .NET.
使用 VBA,您可以创建自己的类,并且可以从这些类创建对象。但是,VBA 不支持继承,也不真正支持在 OO 语言(如 C++ 或 .NET)中使用的术语经典含义中的“多态性”。
VBA classes do support encapsulation, and abstraction.
VBA 类确实支持封装和抽象。
回答by JKAbrams
One particular shortcoming in VBA is in the encapsulation of arrays of objects.
VBA 中的一个特殊缺点是对象数组的封装。
You can have arrays of objects, but you cannot go more than one level down. Workarounds exists, for example by using the Variant type but then you lose type safety. This makes it cumbersome to work with hierarchical object structures, while possible you will end up with convoluted code.
您可以拥有对象数组,但不能向下超过一层。存在变通方法,例如通过使用 Variant 类型,但您会失去类型安全性。这使得使用分层对象结构变得很麻烦,同时您可能最终会得到复杂的代码。