VB6成员变量继承

4

我在继承一个(公共的)变量时遇到了问题,假设变量名为

Public Var As ClassThatIsIndependent

上面的声明本身没有问题,但是如果我继承了包含它的类,则可能会出现问题。
Implements BaseClass

我得到了错误信息“对象模块需要实现接口变量”。我已经尝试了以下选项(都在ChildClass内部):
Public Var As ClassThatIsIndependent

并且

Public BaseClass_Var As ClassThatIsIndependent

但是它们都没能解决问题。有什么替代方案吗?我可以接受可能的Set/Get解决方案,但是我更希望将Var保持为公共变量。

1个回答

7
根据Visual Basic 6.0程序员指南中的多态性,实现属性部分:Implementing Properties

Suppose we give the Animal class an Age property, by adding a Public variable to the Declarations section:

Option Explicit
Public Age As Double

The Procedure drop downs in the code modules for the Tyrannosaur and Flea classes now contain property procedures for implementing the Age property,

Using a public variable to implement a property is strictly a convenience for the programmer. Behind the scenes, Visual Basic implements the property as a pair of property procedures.

You must implement both procedures. The property procedures are easily implemented by storing the value in a private data member, as shown here:

Private mdblAge As Double

Private Property Get Animal_Age() As Double
   Animal_Age = mdblAge
End Property

Private Property Let Animal_Age(ByVal RHS As Double)
   mdblAge = RHS
End Property

The private data member is an implementation detail, so you have to add it yourself.

也就是说,“公共接口”无论您使用公共变量还是使用Property Get/Let定义,它们都是完全相同的。要在接口中实现属性,您不能使用公共变量方法,而需要使用Property Get/Let语法并在自己的私有变量中处理数据存储。

1
近20年来,人们仍然从未想过翻开手册。真奇怪。到现在他们应该意识到VB6拥有一些最好的文档,虽然不完美但远比大多数其他语言要好。这可能是为什么VB6总是在Tiobe的“求助”排名中得分如此低的原因之一。 - Bob77
1
{btsdaf} - user65839
无论你喜欢还是讨厌微软,他们从来没有文档问题(现在也没有)。 - Hardryv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接