(200-06-21) VB.NET 類別規劃 繼承 OverLoading

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

OverLoading 多載 參數數量不一樣 或 相同參數數量但形態不一樣

父類別 Employee

 

子類別 Sales

 

主程式

 

Module TestSales
    '主程式
    Public Sub Main()
        '定義區域變數
        Dim s As Sales = New Sales() '預設建構子
        s.Id = "0001"
        s.Name = "eric"
        s.Salary = 20000
        s.Qa = 50000
        System.Console.WriteLine(s.Name)
        '算業績
        's.calBon(60000)
        '薪資計算(呼叫到Employee)
        Dim sal As Decimal = s.calSalary()
        System.Console.WriteLine(s.Salary)
        '呼叫另一個含業績獎金的
        s.calSalary(60000)

        System.Console.WriteLine(s.Salary)

    End Sub
End Module
'指定繼承來源(Parent Class) 非關命名空間
Imports mod06.Domain.IT
Public Class Sales
    Inherits Employee '單一繼承(父親只能一個)
    'Data Field
    '業績
    Private _qa As Decimal
    Private _bon As Decimal '獎金

    '屬性
    ''' <summary>
    ''' 設定或者取得業績
    ''' </summary>
    ''' <value>業績金額</value>
    ''' <returns>業績金額</returns>
    ''' <remarks></remarks>
    Public Property Qa() As Decimal
        Get
            Return Me._qa
        End Get
        Set(ByVal value As Decimal)
            If (value > 0) Then
                Me._qa = value
            End If
        End Set
    End Property
    '唯讀屬性
    Public ReadOnly Property Bon() As Decimal
        Get
            Return Me._bon
        End Get
    End Property
    'Method商業規則Business Rules 獎金計算部分
    Public Sub calBon(ByVal act As Decimal)
        If (act >= Me._qa) Then
            Me._bon = act * 0.05
        End If
    End Sub
    '同時計算獎金與薪資
    Public Overloads Sub calSalary(ByVal act As Decimal)
        If (act >= Me._qa) Then
            Me._bon = act * 0.05
            '底薪_salary加回獎金
            _salary += Me._bon
        End If

    End Sub
End Class
Namespace Domain.IT
    'public Modifier修飾詞-可見度
    '編譯產生父類別為System.Object
    Public Class Employee
        '類別層級變數(Data Field)
        Private _id As String
        Private _name As String
        '實現封裝特性(private 私用 -可見度限這一個類別內)
        '改成protected 物件封裝性 子類別不封裝 
        Protected _salary As Decimal
        Private _birthDate As DateTime
        Private _sex As String
        ' 編譯產生預設建構子 空參數


        '屬性存取(程序)
        Public Property Salary() As Decimal
            Get
                Return _salary
            End Get
            Set(ByVal value As Decimal)
                If (value > 0) Then
                    _salary = value
                End If
            End Set
        End Property

        Public Property Id() As String
            Get
                Return Me._id
            End Get
            Set(ByVal value As String)
                Me._id = value

            End Set
        End Property

        Public Property Name() As String
            Get
                Return Me._name
            End Get
            Set(ByVal value As String)
                Me._name = value
            End Set
        End Property

        Public Property BirthDate() As DateTime
            Get
                Return Me._birthDate
            End Get
            Set(ByVal value As DateTime)
                If (value <= System.DateTime.Now) Then
                    Me._birthDate = value
                End If
            End Set
        End Property

        Public Property Sex() As String
            Get
                Return Me._sex
            End Get
            Set(ByVal value As String)
                Me._sex = value
            End Set
        End Property


        '薪資計算的功能Method
        '定結構 非關細部操作
        Public Overloads Function calSalary() As Decimal
            Return Me._salary
        End Function
    End Class
End Namespace