简体   繁体   English

模块与vba中的面向对象编程

[英]Module vs object-oriented programming in vba

My first "serious" language was Java, so I have comprehended object-oriented programming in sense that elemental brick of program is a class. 我的第一个“严肃”语言是Java,所以我理解了面向对象编程,因为程序的元素块是一个类。 Now I write on VBA and Python. 现在我写VBA和Python。 There are module languages and I am feeling persistent discomfort: I don't know how should I decompose program in a modules/classes. 有模块语言,我感到持续的不适:我不知道如何在模块/类中分解程序。

I understand that one module corresponds to one knowledge domain, one module should ba able to test separately... Should I apprehend module as namespace(c++) only? 我知道一个模块对应一个知识域,一个模块应该能够单独测试......我应该只将模块理解为命名空间(c ++)吗?

I don't do VBA but in python, modules are fundamental. 我不做VBA但是在python中,模块是基础。 As you say, the can be viewed as namespaces but they are also objects in their own right. 正如您所说,可以将其视为命名空间,但它们本身也是对象。 They are not classes however, so you cannot inherit from them (at least not directly). 但它们不是类,所以你不能从它们继承(至少不是直接)。

I find that it's a good rule to keep a module concerned with one domain area. 我发现保留一个涉及一个域区域的模块是一个很好的规则。 The rule that I use for deciding if something is a module level function or a class method is to ask myself if it could meaningfully be used on any objects that satisfy the 'interface' that it's arguments take. 我用来判断某些东西是模块级函数还是类方法的规则是问自己是否可以在任何满足它所参数的“接口”的对象上有意义地使用它。 If so, then I free it from a class hierarchy and make it a module level function. 如果是这样,那么我将它从类层次结构中释放出来并使其成为模块级函数。 If its usefulness truly is restricted to a particular class hierarchy, then I make it a method. 如果它的用处真正局限于特定的类层次结构,那么我将它作为一种方法。

If you need it work on all instances of a class hierarchy and you make it a module level function, just remember that all the the subclasses still need to implement the given interface with the given semantics. 如果您需要它可以在类层次结构的所有实例上工作并且使它成为模块级函数,请记住所有子类仍然需要使用给定的语义实现给定的接口。 This is one of the tradeoffs of stepping away from methods: you can no longer make a slight modification and call super . 这是退出方法的权衡之一:你不能再进行轻微修改并调用super On the other hand, if subclasses are likely to redefine the interface and its semantics, then maybe that particular class hierarchy isn't a very good abstraction and should be rethought. 另一方面,如果子类可能重新定义接口及其语义,那么也许特定的类层次结构不是一个非常好的抽象,应该重新考虑。

It is matter of taste. 这是品味的问题。 If you use modules your 'program' will be more procedural oriented. 如果你使用模块,你的'程序'将更加面向程序。 If you choose classes it will be more or less object oriented. 如果选择类,它将或多或少是面向对象的。 I'm working with Excel for couple of months and personally I choose classes whenever I can because it is more comfortable to me. 我正在使用Excel几个月,我个人会尽可能选择课程,因为它对我来说更舒服。 If you stop thinking about objects and think of them as Components you can use them with elegance. 如果您不再考虑objects并将其视为Components ,则可以优雅地使用它们。 The main reason why I prefer classes is that you can have it more that one. 我更喜欢课程的主要原因是你可以拥有更多的课程。 You can't have two instances of module. 您不能拥有两个模块实例。 It allows me use encapsulation and better code reuse. 它允许我使用封装和更好的代码重用。

For example let's assume that you like to have some kind of logger, to log actions that were done by your program during execution. 例如,假设您希望拥有某种记录器,以记录程序在执行期间执行的操作。 You can write a module for that. 你可以为此编写一个模块。 It can have for example a global variable indicating on which particular sheet logging will be done. 它可以具有例如全局变量,该变量指示将在哪个特定纸张记录上完成。 But consider the following hypothetical situation: your client wants you to include some fancy report generation functionality in your program. 但请考虑以下假设情况:您的客户希望您在程序中包含一些奇特的报告生成功能。 You are smart so you figure out that you can use your logging code to prepare them. 你很聪明,所以你发现你可以使用你的日志代码来准备它们。 But you can't do log and report simultaneously by one module. 但是你不能通过一个模块同时进行日志和报告。 And you can with two instances of logging Component without any changes in their code. 您可以使用两个日志记录Component实例,而无需更改代码。

Idioms of languages are different and thats the reason a problem solved in different languages take different approaches. 语言的习语是不同的,这就是在不同语言中解决问题的原因采用不同的方法。

  1. "C" is all about procedural decomposition. “C”是关于程序分解的。
  2. Main idiom in Java is about "class or Object" decomposition. Java中的主要习语是“类或对象”分解。 Functions are not absent, but they become a part of exhibited behavior of these classes. 功能不存在,但它们成为这些类的展示行为的一部分。
  3. "Python" provides support for both Class based problem decomposition as well as procedural based. “Python”支持基于类的问题分解以及基于程序的问题。

All of these uses files, packages or modules as concept for organizing large code pieces together. 所有这些都使用文件,包或模块作为组合大型代码片段的概念。 There is nothing that restricts you to have one module for one knowledge domain. 没有什么可以限制您为一个知识域创建一个模块。 These are decomposition and organizing techniques and can be applied based on the problem at hand. 这些是分解和组织技术,可以根据手头的问题进行应用。

If you are comfortable with OO, you should be able to use it very well in Python. 如果你对OO感到满意,你应该能够在Python中很好地使用它。

VBA also allows the use of classes. VBA还允许使用类。 Unfortunately, those classes don't support all the features of a full-fleged object oriented language. 不幸的是,这些类不支持全面的面向对象语言的所有功能。 Especially inheritance is not supported. 特别是不支持继承。

But you can work with interfaces, at least up to a certain degree. 但是你可以使用接口,至少在某种程度上。

I only used modules like "one module = one singleton". 我只使用了“one module = one singleton”这样的模块。 My modules contain "static" or even stateless methods. 我的模块包含“静态”甚至无状态方法。 So in my opinion a VBa module is not namespace. 所以我认为VBa模块不是命名空间。 More often a bunch of classes and modules would form a "namespace". 更常见的是,一堆类和模块将形成“命名空间”。 I often create a new project (DLL, DVB or something similar) for such a "namespace". 我经常为这样的“命名空间”创建一个新项目(DLL,DVB或类似的东西)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM