您的位置:首页 > 编程语言 > VB

2009-03-20 VB定义接口

2009-03-20 21:54 337 查看
C#定义接口大家都接触比较多,但VB定义接口大家可能接触比较少。下面就定义VB接口,结合一个工程讲一下:

1. 新建一个VB EXE 工程。

2. 添加一个类,命名为ITest, 代码为:

Option Explicit

Public Function TestFunction() As Boolean

End Function

3. 添加一个类,命名为clsTest1,代码为:

Option Explicit

Implements ITest

Private Function ITest_TestFunction() As Boolean

MsgBox "This is clsTest1 to implement interface ITest.", vbOKOnly, "VBInterface"

End Function

4. 添加一个类,命名为clsTest2,代码为:

Option Explicit

Implements ITest

Private Function ITest_TestFunction() As Boolean

MsgBox "This is clsTest2 to implement interface ITest.", vbOKOnly, "VBInterface"

End Function

5. 添加一个窗体,命名为frmMain,在窗体上放置两个Button,并命名为btnTest1,btnTest2.窗体的代码为:

Option Explicit

Private Sub cmdTest1_Click()

Dim Test1 As ITest

Set Test1 = New clsTest1

Call Test1.TestFunction

End Sub

Private Sub cmdTest2_Click()

Dim Test2 As ITest

Set Test2 = New clsTest2

Call Test2.TestFunction

End Sub

6. 运行程序,点击两个Button,就可以看到两个不同实现类的响应。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: