无法在VB6中实现一个类

Bri*_*per 5 vb6

我正在尝试在VB6中实现一个接口.我已经定义了Cast_Speed这样的类......

Public Function Run_Time() As Long

End Function
Run Code Online (Sandbox Code Playgroud)

和这样的实现......

Option Explicit
Implements Cast_Speed

Public Function Cast_Speed_Run_Time() As Long
    Cast_Speed_Run_Time = 0
End Function
Run Code Online (Sandbox Code Playgroud)

但是试图编译它会让'对象模块需要为接口'Cast_Speed'实现'Run_Time'.谁能看到我做错了什么?我的子程序似乎很好,但我尝试的所有功能都有这个问题.

one*_*hen 14

它不喜欢方法名称中的下划线字符.请尝试使用RunTime().

我只是在没有下划线的情况下测试它,它对我来说很好用:

'// class Cast_Speed
Option Explicit

Public Function RunTime() As Long

End Function


'// class Class1
Option Explicit

Implements Cast_Speed

Public Function Cast_Speed_RunTime() As Long
  Cast_Speed_RunTime = 0
End Function
Run Code Online (Sandbox Code Playgroud)

  • +1来自VB6文档*创建用于Implements语句的接口*`接口方法在其名称中不能有下划线http://msdn.microsoft.com/en-us/library/aa262287(VS.60).aspx (5认同)

Bob*_*des 5

虽然您可以将接口实现公开,但它不被认为是一种好的做法,仅仅是允许直接实例化接口被认为是一种良好的做法,您也可以这样做.它只是一个例子,可以在VB6中编写极其糟糕的代码.:)

最佳做法如下:

  1. 接口实例化属性是PublicNotCreatable.
  2. 已实现的接口方法的作用域为Private.

从而:

Dim x as iMyInterface
Set x = new MyiMyInterfaceImplementation
x.CalliMyInterfaceMethodA
x.CalliMyInterfaceMethodY
Run Code Online (Sandbox Code Playgroud)

等等.如果有人试图直接实例化接口,那么应该会导致错误,并且如果有人试图直接调用已实现的方法而不是多态通过应该返回错误的接口.