實作介面多型範例
'實作介面多型
'先建立一個介面 ps一般介面習慣用I開頭命名
Public Interface IMut
Function A() As String
Function A(ByRef pA As String) As String
End Interface
'建立IIMutA1實作IMut
Public Class IIMutA1
Implements IMut
Public Function A() As String Implements IMut.A
Return "IIMutA1"
End Function
Public Function A(ByRef pA As String) As String Implements IMut.A
Return "IIMutA1"
End Function
End Class
'建立IIMutA2實作IMut
Public Class IIMutA2
Implements IMut
Public Function A() As String Implements IMut.A
Return "IIMutA2"
End Function
Public Function A(ByRef pA As String) As String Implements IMut.A
Return "IIMutA2"
End Function
End Class
Public Class MutIclass
Public Sub MMCCI(ByRef use As String)
'先宣告MutAA為剛剛建立的介面IMut
Dim MutAA As IMut
'以丟進來的參數判斷要New哪一個Class
Select Case use
Case "IIMut1"
MutAA = New IIMutA1
Case "IIMut2"
MutAA = New IIMutA2
End Select
MutAA.A() '這個會依照不同的Class有不同的回傳值
'ps 多型的定義是同名異式
End Sub
End Class