gpt4 book ai didi

oop - 在 VBA 中实现我自己的接口(interface) - 错误 : Object module needs to implement 'x' for interface 'y'

转载 作者:行者123 更新时间:2023-12-01 18:35:06 25 4
gpt4 key购买 nike

如何实现我的类ClsInterface,其中包含以下代码:

Public Function add(x As Integer, y As Integer) As Integer
End Function

在我的类Class2中,其中包含以下代码:

Implements ClsInterface

Public Function add(x As Integer, y As Integer) As Integer
add = x + y
End Function

我的测试代码是

Public Sub test()
Dim obj As New Class2
MsgBox obj.add(5, 2)
End Sub

这总是会出现以下错误:

Microsoft Visual Basic
Compile error:

Object module needs to implement 'add' for interface 'ClsInterface'
OK/Help

但 Microsoft 帮助中没有任何帮助(当我按下“帮助”按钮时)。

有什么想法吗?

最佳答案

您的 Class2 必须如下所示:

Implements ClsInterface

Private Function ClsInterface_add(x As Integer, y As Integer) As Integer
ClsInterface_add = x + y
End Function

查看Class2代码窗口顶部的下拉框,你可以看到你可以引用哪些基础对象; ClsInterface

在您想要的测试代码中:

Dim obj As New ClsInterface

如果你想跨接口(interface)调用。

我还建议以 ISomeDescription 的形式命名接口(interface),并使用 Dim 然后 Set 而不是 Dim As New >.

关于oop - 在 VBA 中实现我自己的接口(interface) - 错误 : Object module needs to implement 'x' for interface 'y' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3171324/

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