Board logo

標題: VB.NET 類別規劃 繼承 OverLoading [打印本頁]

作者: narita    時間: 2011-9-6 00:50     標題: VB.NET 類別規劃 繼承 OverLoading

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

父類別 Employee



子類別 Sales



主程式

Module TestSales  

02     '主程式  

03     Public Sub Main()  

04         '定義區域變數  

05         Dim s As Sales = New Sales() '預設建構子  

06         s.Id = "0001"

07         s.Name = "eric"

08         s.Salary = 20000  

09         s.Qa = 50000  

10         System.Console.WriteLine(s.Name)  

11         '算業績  

12         's.calBon(60000)  

13         '薪資計算(呼叫到Employee)  

14         Dim sal As Decimal = s.calSalary()  

15         System.Console.WriteLine(s.Salary)  

16         '呼叫另一個含業績獎金的  

17         s.calSalary(60000)  

18   

19         System.Console.WriteLine(s.Salary)  

20   

21     End Sub

22 End Module
view sourceprint?01 '指定繼承來源(Parent Class) 非關命名空間  

02 Imports mod06.Domain.IT  

03 Public Class Sales  

04     Inherits Employee '單一繼承(父親只能一個)  

05     'Data Field  

06     '業績  

07     Private _qa As Decimal

08     Private _bon As Decimal '獎金  

09   

10     '屬性  

11     ''' <summary>  

12     ''' 設定或者取得業績  

13     ''' </summary>  

14     ''' <value>業績金額</value>  

15     ''' <returns>業績金額</returns>  

16     ''' <remarks></remarks>  

17     Public Property Qa() As Decimal

18         Get

19             Return Me._qa  

20         End Get

21         Set(ByVal value As Decimal)  

22             If (value > 0) Then

23                 Me._qa = value  

24             End If

25         End Set

26     End Property

27     '唯讀屬性  

28     Public ReadOnly Property Bon() As Decimal

29         Get

30             Return Me._bon  

31         End Get

32     End Property

33     'Method商業規則Business Rules 獎金計算部分  

34     Public Sub calBon(ByVal act As Decimal)  

35         If (act >= Me._qa) Then

36             Me._bon = act * 0.05  

37         End If

38     End Sub

39     '同時計算獎金與薪資  

40     Public Overloads Sub calSalary(ByVal act As Decimal)  

41         If (act >= Me._qa) Then

42             Me._bon = act * 0.05  

43             '底薪_salary加回獎金  

44             _salary += Me._bon  

45         End If

46   

47     End Sub

48 End Class
view sourceprint?01 Namespace Domain.IT  

02     'public Modifier修飾詞-可見度  

03     '編譯產生父類別為System.Object  

04     Public Class Employee  

05         '類別層級變數(Data Field)  

06         Private _id As String

07         Private _name As String

08         '實現封裝特性(private 私用 -可見度限這一個類別內)  

09         '改成protected 物件封裝性 子類別不封裝   

10         Protected _salary As Decimal

11         Private _birthDate As DateTime  

12         Private _sex As String

13         ' 編譯產生預設建構子 空參數  

14   

15   

16         '屬性存取(程序)  

17         Public Property Salary() As Decimal

18             Get

19                 Return _salary  

20             End Get

21             Set(ByVal value As Decimal)  

22                 If (value > 0) Then

23                     _salary = value  

24                 End If

25             End Set

26         End Property

27   

28         Public Property Id() As String

29             Get

30                 Return Me._id  

31             End Get

32             Set(ByVal value As String)  

33                 Me._id = value  

34   

35             End Set

36         End Property

37   

38         Public Property Name() As String

39             Get

40                 Return Me._name  

41             End Get

42             Set(ByVal value As String)  

43                 Me._name = value  

44             End Set

45         End Property

46   

47         Public Property BirthDate() As DateTime  

48             Get

49                 Return Me._birthDate  

50             End Get

51             Set(ByVal value As DateTime)  

52                 If (value <= System.DateTime.Now) Then

53                     Me._birthDate = value  

54                 End If

55             End Set

56         End Property

57   

58         Public Property Sex() As String

59             Get

60                 Return Me._sex  

61             End Get

62             Set(ByVal value As String)  

63                 Me._sex = value  

64             End Set

65         End Property

66   

67   

68         '薪資計算的功能Method  

69         '定結構 非關細部操作  

70         Public Overloads Function calSalary() As Decimal

71             Return Me._salary  

72         End Function

73     End Class

74 End Namespace




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)