VB6成员变量继承
VB6 Member variable inheritance
我在继承 (public) 变量时遇到问题,比方说
Public Var As ClassThatIsIndependent
上面的声明本身不会产生任何问题,但是,如果我继承了包含它的 class
Implements BaseClass
我收到错误 "object module needs to implement variable for interface"。我已经尝试了这些选项(都在 ChildClass 中)
Public Var As ClassThatIsIndependent
和
Public BaseClass_Var As ClassThatIsIndependent
但是其中 none 解决了问题。还有其他选择吗?我对可能的 Set/Get 解决方案持开放态度,但是,我更愿意将 Var 维护为 public 变量。
根据 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.
也就是说,无论您使用 Public 变量还是使用 属性 Get/Let 定义它们,"public interface" 都完全相同。要在接口中实现 属性,您不能使用 Public 变量方法,需要使用 属性 Get/Let 语法并在其中处理数据存储你自己的私有变量。
我在继承 (public) 变量时遇到问题,比方说
Public Var As ClassThatIsIndependent
上面的声明本身不会产生任何问题,但是,如果我继承了包含它的 class
Implements BaseClass
我收到错误 "object module needs to implement variable for interface"。我已经尝试了这些选项(都在 ChildClass 中)
Public Var As ClassThatIsIndependent
和
Public BaseClass_Var As ClassThatIsIndependent
但是其中 none 解决了问题。还有其他选择吗?我对可能的 Set/Get 解决方案持开放态度,但是,我更愿意将 Var 维护为 public 变量。
根据 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.
也就是说,无论您使用 Public 变量还是使用 属性 Get/Let 定义它们,"public interface" 都完全相同。要在接口中实现 属性,您不能使用 Public 变量方法,需要使用 属性 Get/Let 语法并在其中处理数据存储你自己的私有变量。