gpt4 book ai didi

variables - VB6成员变量继承

转载 作者:行者123 更新时间:2023-12-02 23:04:10 26 4
gpt4 key购买 nike

假设我在继承(公共(public))变量时遇到问题

Public Var As ClassThatIsIndependent

上面的声明本身不会产生任何麻烦,但是,如果我继承持有它的类

Implements BaseClass

我收到错误“对象模块需要实现接口(interface)变量”。我已经尝试过这些选项(都在 ChildClass 内)

Public Var As ClassThatIsIndependent

Public BaseClass_Var As ClassThatIsIndependent

但是他们都没有解决问题。还有其他选择吗?我对可能的 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 变量还是使用 Property Get/Let 定义它们,“公共(public)接口(interface)”都是完全相同的。并且要在接口(interface)中实现属性,您不能使用公共(public)变量方法,而是需要使用 Property Get/Let 语法并在您自己的私有(private)变量中处理其数据存储。

关于variables - VB6成员变量继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46900550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com