gpt4 book ai didi

inheritance - VB6在子类中访问父类的重载接口(interface)子程序

转载 作者:行者123 更新时间:2023-12-03 00:21:13 25 4
gpt4 key购买 nike

据我所知,VB6不支持继承,但它支持接口(interface)。我正在尝试创建一个重载子例程,将其信息传递给基类的同名子例程。

Sub Main()
Dim Student1 as New Student
Dim Teacher1 as New Person

Teacher1.Init "Mr. Smith"
Student1.Init "John Doe", Teacher1
End Sub

' --------------------------
' Class definition of Person
Dim name as String

Public Sub Init(n as String)
name = n
End Sub

' --------------------------
' Class definition of Student
Implements Person

Dim Teacher as Person

Public Sub Init(n as String, t as Person)
' I could use the following line to assign the name
name = n
' but I want to be able to call the Init(n) of the parent class

Set Teacher = t
End Sub

最佳答案

抱歉,在 VB6 中不能这样做。您也不能将所有重载放入界面中,因为您会收到“检测到不明确的名称:Init”错误。

但是,您可以将您的教师引用添加到人员作为自引用,并且当此人是教师时,只需传递相同的实例即可。像这样:

Dim name as String
Dim teacher as Person

Public Sub Init(n as String, t as Person)
name = n
teacher = t
End Sub

然后:

Dim Student1 as New Person
Dim Teacher1 as New Person

Teacher1.Init "Mr. Smith", Teacher1
Student1.Init "John Doe", Teacher1

虽然它相当困惑(例如,您可以在传入的教师实例上调用 Init 方法,但这是丑陋的),但这是您在 VB6 中可以做的最好的事情。

关于inheritance - VB6在子类中访问父类的重载接口(interface)子程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28548316/

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