(200-06-25) VB.NET 類別規劃 多型

摘要:(200-06-21) VB.NET 類別規劃 多型

多型化 --> "Class" "多型化 介面"

  1. 繼承架構
  2. Overriding Method(VMI:virtual method invoice) [虛擬功能臨架]
  3. 父類別變數 參考 子類別的物件

 

Imports mod06.Domain.IT
Module TestPoly
    Public Sub Main()
        '收集多種員工(原型Employee)
        Dim col As New System.Collections.Generic.List(Of Employee)()
        '陣列
        Dim emp(3) As Employee

        col.Add(New Employee("001", "eric", New DateTime(2000, 1, 1), "男性"))
        col.Add(New Sales("0002", "linda", New DateTime(2000, 1, 1), "女性", 10000D))
        col.Add(New Engineer())

        emp(0) = New Employee("001", "eric", New DateTime(2000, 1, 1), "男性")
        emp(1) = New Sales("0002", "linda", New DateTime(2000, 1, 1), "女性", 10000D)
        emp(2) = New Engineer()
        emp(3) = New Employee("004", "bill", New DateTime(2000, 1, 1), "男性")

        '要開始工作了
        For Each e As Employee In emp
            '相對員工(通稱Employee)
            '問是否為業務員
            If TypeOf e Is Sales Then
                '強制轉型
                DirectCast(e, Sales).calBon(20000)
            End If
            '薪資資算
            e.calSalary()

            '問薪資
            System.Console.WriteLine(e.Salary)
        Next
    End Sub
End Module