本文介绍了VBA继承,超级模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有A类实现B类



--- A类----

  implements B 
public sub B_do()
end sub

- B类----

  public sub do()
end sub

如何从A调用do()? (super.do())那么,我如何可以为这两个类定义一些常用变量?现在我可以继承只有函数,子和属性...



添加:同样的问题



添加:不可能在hierarhy类中共享变量。您应该实现属性(与函数相同)。

解决方案

在VBA中通常的方法是让A包含B的实例以及A实现B的接口,然后将A的B接口的调用委托给内部B。



这是旧的东西,但是看到Visual Studio 6.0程序员指南:





有一篇关于代码重用的许多(Inter)Faces的章节描述了这个约定:





MS描述的方式是:

这意味着实现继承需要大量的显式授权方法。甚至有一章小标题:这不会变得乏味吗?另一个原因是VBA中的OOP是一个PITA(TM)...



编辑不会附加在评论中:



要回答你的评论中提出的问题,那么A 是一个B.当你使A实现B的界面时,你本质上就是说可以对待一个好像它实际上是类型B.在VBA中,你所做的方式是通过声明一个类型B的变量,然后将其设置为A的一个实例。VBA将知道当你像B一样调用它时该怎么做:

  Dim usedAsB as B 
Dim anA as A

Set anA = New A
设置usedAsB = anA'因为A实现B

usedAsB.something()'将调用类A中定义的B_something()

至于你在调试窗口中看到的,我不知道为什么会这样。就强制代表团来说,我不知道你的意思。 VBA会自动将调用B接口传递给A类中的正确方法。如果您的意思是自动生成代码以上述方式继承B的实现,那么我就不了解VBA。我认为VB6的各种专业版本可以做到这一点,但我从来没有使用VB6,所以我不知道。


For example I have class A which implements class B

---class A----

implements B
public sub B_do()
end sub

--class B----

public sub do()
end sub

How can I call do() from A? (super.do()) So, how I can define some common variable for both classes? Now I can inherit only functions, sub and properties...

added: same question http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/5a83d794-3da1-466a-83d3-5d2eb0a054b2

added: It is not possible to share variable across hierarhy of classes. You should implements property (same way as functions).

解决方案

The usual way to do this in VBA is to have A contain an instance of B as well as having A implement B's interface, and then delegate calls to the B interface of A to the internal B.

This is old stuff, but see the Visual Studio 6.0 Programmer's Guide:

http://msdn.microsoft.com/en-us/library/aa716285(VS.60).aspx

There is a chapter on "The Many (Inter)Faces of Code Reuse" that describes this convention:

http://msdn.microsoft.com/en-us/library/aa240846(v=VS.60).aspx

The way MS describes it is:

This means that implementation inheritance requires lots of explicit delegation methods. There's even a chapter subheading: "Doesn't This Get Tedious?". Yet another reason why OOP in VBA is a PITA (TM)...

EDIT THAT WON'T FIT IN A COMMENT:

To answer the question you posed in your comment, well, an A is a B. When you make A implement B's interface, you are essentially saying that you can treat an instance of A as if it is actually of type B. In VBA, the way you do that is by declaring a variable of type B, and then setting it to an instance of A. VBA will know what to do when you call it like a B:

Dim usedAsB as B
Dim anA as A

Set anA = New A
Set usedAsB = anA    'fine since A implements B

usedAsB.something()    'will call B_something() defined in class A

As far as what you see in the debug window, I don't why it appears that way. And as far as forced delegation, I'm not sure what you mean. VBA automatically dispatches calls to the B interface to the right methods in the A class. If you mean automatically generating the code to inherit B's implementation in the manner described above, there's nothing like that I know of for VBA. I think the various "professional" versions of VB6 could do that, but I've never used VB6 so I don't know.

这篇关于VBA继承,超级模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:36