vb.net接口(interface)
- 格式:pdf
- 大小:32.24 KB
- 文档页数:1
vb.net接⼝(interface)
⽤来实现不同类中,⽤统⼀⽅法来调⽤类中的各⾃⽅法和属性。
可以通过CTYPE(类名,接⼝名).接⼝⽅法来访问接⼝中的⽅法。
实例说明:
⼀个模块⽂件中写⼊
Public Interface 接⼝1
ReadOnly Property id() As Long '定义⼀个只读的属性
Function onll(byval en As String ) As Boolean '定义⼀个过程
End Interface
Class BaseClass1: Implements 接⼝1
Private m_id As Long=3
Public readonly Property idi As Long Implements 接⼝1.id
Get
Return m_id
End Get
End Property
Public Function onll(en As String ) As Boolean Implements 接⼝1.onll
If en="接⼝" Then
m_id+=1
Return True
Else
Return False
End If
End Function
End Class
Class BaseClass2: Implements 接⼝1
Private m_id As Long=3
Public readonly Property idi As Long Implements 接⼝1.id
Get
Return m_id
End Get
End Property
Public Function onl(en As String ) As Boolean Implements 接⼝1.onll
If en="接⼝x" Then
m_id+=1
Return True
Else
Return False
End If
End Function
End Class
Public Sub showis_id(p As 接⼝1)'定义接⼝实现⽅法
p.onll("接⼝") '传递给各⾃类中的⽅法
MsgBox (p.id.ToString )'显⽰各⾃类中的ID.
End sub
在过程中加⼊:
Dim ss As BaseClass1=New BaseClass1
Dim s1 As BaseClass2 =New BaseClass2
showis_id(s1)
showis_id(ss)
类就会实现各⾃类中的⽅法,不必要考虑类中的哪个⽅法在实现。