實作繼承多型範例
'實作繼承多型範例
'建立一個抽象類別
Public MustInherit Class Mut
'建立抽象方法,必須被複寫
Public MustOverride Function A() As String
Public MustOverride Function A(ByRef pA As String) As String
End Class
'建立MutA1繼承Mut
Public Class MutA1
Inherits Mut
'相同名稱 不同參數 稱為多載
Public Overloads Overrides Function A() As String
Return "MutA1"
End Function
Public Overloads Overrides Function A(ByRef pA As String) As String
Return "MutA1"
End Function
End Class
'建立MutA2繼承Mut
Public Class MutA2
Inherits Mut
Public Overloads Overrides Function A() As String
Return "MutA2"
End Function
Public Overloads Overrides Function A(ByRef pA As String) As String
Return "MutA2"
End Function
End Class
Public Class Mutclass
Public Sub MMCC(ByRef use As String)
'先宣告MutAA為剛剛建立的抽象類別Mut
Dim MutAA As Mut
'以丟進來的參數判斷要New哪一個Class
Select Case use
Case "Mut1"
MutAA = New MutA1
Case "Mut2"
MutAA = New MutA2
End Select
MutAA.A() '這個會依照不同的Class有不同的回傳值
'ps 多型的定義是同名異式
End Sub
End Class