我有一个朋友,他学习 VB.NET 时间还不是很长。这几天,他对面向对象编程中的接口突然产生了很大的兴趣。为了帮助他让他能够对接口有一个初步的、正确的认识,我写了下面的例子。同时,希望本例对目前还不是很了解接口的朋友有所帮助。
VB.NET接口范例
在这个例子中,我定义了一个名为 ISpeak 的接口,它其中包括了三个签名(请参见下面程序)。由于接口是代表一种约定或者说是一种规则,它并不包含这种约定或者规则的具体实现。所以,我们只需要在接口中定义签名即可。在该接口中,包括了三个成员签名,它们分别代表了属性、方法以及函数。
接下来,我又定义了三个类,分别是 Baby、Children 和 Adlut ,这个三类都实现了上述接口。并且,在每个类实现接口的成员时,都给出了具体的实现。
来看代码:
'定义一个接口
Public Interface ISpeak
'仅定义签名,不包含任何实现
ReadOnly Property CanSpeak() As Boolean '属性
Sub Speak() '方法
Function GetSpeakLevel() As SpeakLevelEnum '函数
End Interface
'说话水平枚举
Public Enum SpeakLevelEnum
Bad '很差
Ordinary ‘一般
Fluent '流利
End Enum
Public Class Baby : Implements ISpeak '通过 Implements 关键字实现接口
'以下是实现该接口的所有成员
Public ReadOnly Property CanSpeak() As Boolean Implements ISpeak.CanSpeak
Get
Return False
End Get
End Property
Public Function GetSpeakLevel() As SpeakLevelEnum Implements ISpeak.GetSpeakLevel
Return SpeakLevelEnum.Bad
End Function
Public Sub Speak() Implements ISpeak.Speak
Console.WriteLine("(旁白):他是一个婴儿,还不会说话。")
End Sub
End Class
Public Class Children : Implements ISpeak
Public ReadOnly Property CanSpeak() As Boolean Implements ISpeak.CanSpeak
Get
Return True
End Get
End Property
Public Function GetSpeakLevel() As SpeakLevelEnum Implements ISpeak.GetSpeakLevel
Return SpeakLevelEnum.Ordinary
End Function
Public Sub Speak() Implements ISpeak.Speak
Console.WriteLine("哈哈,我是一个快乐的孩子!")
End Sub
End Class
Public Class Adult : Implements ISpeak
Public ReadOnly Property CanSpeak() As Boolean Implements ISpeak.CanSpeak
Get
Return True
End Get
End Property
Public Function GetSpeakLevel() As SpeakLevelEnum Implements ISpeak.GetSpeakLevel
Return SpeakLevelEnum.Fluent
End Function
Public Sub Speak() Implements ISpeak.Speak
Console.WriteLine("OK, 我是成年人了,不但能流利地说母语,还会说外语。")
End Sub
End Class
'程序入口点
Public Class AppStart
Shared Sub Main()
Dim person1 As New Baby
Dim person2 As New Children
Dim person3 As New Adult
person1.Speak()
person2.Speak()
person3.Speak()
If person2.GetSpeakLevel > SpeakLevelEnum.Bad Then
Console.WriteLine(" Person2 的说话水平不算很差")
End If
If person1.CanSpeak Then
Console.WriteLine(" Person1 会说话")
Else
Console.WriteLine(" Person1 不会说话")
End If
Console.Read()
End Sub
End Class
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
在 VS2005/2008 中建立一个“控制台项目”项目,将默认的 Module1.vb 删除,在该项目中添加一个类,将上述代码复制到这个类中覆盖原有内容,即可运行调试。
VB.NET接口范例学习经验
关于接口,学习总结如下:
1、定义接口名,应按照惯例,将接口名的***个字母命名为 I,如上例中的 ISpeak,这样做的目的是见名知义,原因是因为接口的英语单词是 Interface;
2、由于接口中的成员签名必须在实现该接口的类中给出其具体实现,所以它们前面不用加访问级别修饰符(Public、Privated 等);
3、类通过 Implements 关键字实现接口;
4、一个类要实现一个接口,就必须实现该接口中定义的所有成员签名;
5、一个类可以实现多个接口,方法是在接口名用“,”隔开,如 Implements ISpeak,IRun。
以上是一个简单易懂的VB.NET接口范例,以及学习心得。
【编辑推荐】