'Visual Basic 2010 Redefinition - Overridable en Overrides Object Oriented Programming Class Class1 Public Overridable Function Method1() As String Method1 = "Class1.Method1()" End Function End Class Class Class2 : Inherits Class1 Public Overrides Function Method1() As String Method1 = "Class2.Method1()" End Function End Class Module Example1 Sub Main() Dim object1 As Class1 = New Class1 Console.WriteLine(object1.Method1()) ' (1) ' Dim object2 As Class2 = New Class2 Console.WriteLine(object2.Method1()) ' (2) ' Console.ReadLine() End Sub End Module Class Class3 Public Overridable Function Method1() As String Method1 = "Class3.Method1()" End Function Public Overridable Function Method1(ByVal argument As String) As String Method1 = "Class3.Method1(" & argument & ")" End Function End Class Class Class4 : Inherits Class3 Public Overrides Function Method1() As String ' (1) Method1 = "Class4.Method1()" End Function Public NotOverridable Overrides Function Method1(ByVal argument As String) _ As String Method1 = "Class4.Method1(" & argument & ")" End Function Public Overloads Function Method1(ByVal argument1 As String, _ ByVal argument2 As String) As String Method1 = "Class4.Method1(" & argument1 & "," & argument2 & ")" End Function End Class Class Class5 : Inherits Class4 Public Overrides Function Method1() As String Method1 = "Class5.Method1()" End Function End Class Module Example2 Sub Main() Dim object1 As Class3 = New Class3 Console.WriteLine(object1.Method1()) Console.WriteLine(object1.Method1("test")) ' Dim object2 As Class4 = New Class4 Console.WriteLine(object2.Method1()) Console.WriteLine(object2.Method1("test")) Console.WriteLine(object2.Method1("test1", "test2")) ' Dim object3 As Class5 = New Class5 Console.WriteLine(object3.Method1()) Console.WriteLine(object3.Method1("test")) Console.WriteLine(object3.Method1("test1", "test2")) ' Console.ReadLine() End Sub End Module Class Counter Protected m_Value As Integer Public ReadOnly Property Value() As Integer Get Value = m_Value End Get End Property Public Overridable Sub Raise() m_Value += 1 End Sub Public Overridable Sub Lower() m_Value -= 1 End Sub End Class Module Example3 Sub Main() Dim counter As Counter = New Counter Console.WriteLine(counter.Value) counter.Raise() Console.WriteLine(counter.Value) counter.Raise() Console.WriteLine(counter.Value) counter.Lower() Console.WriteLine(counter.Value) ' Console.ReadLine() End Sub End Module Class SpecialCounter : Inherits Counter Private m_StepValue As Integer = 1 Public Property StepValue() As Integer Get StepValue = m_StepValue End Get Set(ByVal value As Integer) m_StepValue = value End Set End Property Public Overrides Sub Raise() m_Value += StepValue End Sub Public Overrides Sub Lower() m_Value -= StepValue End Sub End Class Module Example4 Sub Main() Dim specialCounter As SpecialCounter = New SpecialCounter Console.WriteLine(specialCounter.Value) specialCounter.StepValue = 5 specialCounter.Raise() Console.WriteLine(specialCounter.Value) specialCounter.Raise() Console.WriteLine(specialCounter.Value) specialCounter.Lower() Console.WriteLine(specialCounter.Value) ' Console.ReadLine() End Sub End Module Class Person Public Sub New(ByVal name As String) Me.Name = name End Sub Private m_Name As String Public Property Name() As String Get Name = m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property End Class Class Student : Inherits Person Public Sub New(ByVal name As String, ByVal classGroup As String) MyBase.New(name) Me.ClassGroup = classGroup End Sub Private m_ClassGroup As String Public Property ClassGroup() As String Get ClassGroup = m_ClassGroup End Get Set(ByVal value As String) m_ClassGroup = value End Set End Property Public Overrides Function ToString() As String ' (2) ToString = Name & " (" & ClassGroup & ")" End Function End Class Module Example5 Public Sub Main() Dim person1 As Person = New Person("John") Console.WriteLine(person1.ToString()) ' (1) ' Dim student1 As Student = New Student("Jane", "Visual Basic") Console.WriteLine(student1.ToString()) ' (2) ' Console.ReadLine() End Sub End Module Partial Class Counter Public Overrides Function ToString() As String ToString = Value.ToString() End Function End Class Partial Class SpecialCounter Public Overrides Function ToString() As String ToString = MyBase.ToString() & " - Step : " & StepValue.ToString() ' (1) End Function End Class Module Example6 Sub Main() Dim counter1 As Counter = New Counter Console.WriteLine(counter1.ToString()) ' Dim specialCounter1 As SpecialCounter = New SpecialCounter Console.WriteLine(specialCounter1.ToString()) ' Console.ReadLine() End Sub End Module Class Employee Private m_MonthlySalary As Decimal Public Property MonthlySalary() As Decimal Get MonthlySalary = m_MonthlySalary End Get Set(ByVal value As Decimal) m_MonthlySalary = value End Set End Property Public Overridable Function GetYearlySalary() As Decimal GetYearlySalary = MonthlySalary * 12 End Function End Class Class Manager : Inherits Employee Private m_Bonus As Decimal Public Property Bonus() As Decimal Get Bonus = m_Bonus End Get Set(ByVal value As Decimal) m_Bonus = value End Set End Property Public Overrides Function GetYearlySalary() As Decimal GetYearlySalary = MyBase.GetYearlySalary() + Bonus End Function End Class Module Exercise1Solution Public Sub Main() Dim employee1 As Employee = New Employee employee1.MonthlySalary = 1000 Console.WriteLine(employee1.GetYearlySalary()) ' Dim manager1 As Manager = New Manager manager1.MonthlySalary = 2000 manager1.Bonus = 10000 Console.WriteLine(manager1.GetYearlySalary()) ' Console.ReadLine() End Sub End Module Module Exercise2Task Sub Main() Dim addition1 As Addition = New Addition(3, 4) Console.WriteLine(addition1.GetSum()) Console.WriteLine(addition1.ToString()) ' addition1.Operand1 = 5 addition1.Operand2 = 6 Console.WriteLine(addition1.GetSum()) Console.WriteLine(addition1.ToString()) ' Console.ReadLine() End Sub End Module Class Addition Public Sub New(ByVal operand1 As Integer, ByVal operand2 As Integer) Me.Operand1 = operand1 Me.Operand2 = operand2 End Sub Private m_Operand1 As Integer Public Property Operand1() As Integer Get Operand1 = m_Operand1 End Get Set(ByVal value As Integer) m_Operand1 = value End Set End Property Private m_Operand2 As Integer Public Property Operand2() As Integer Get Operand2 = m_Operand2 End Get Set(ByVal value As Integer) m_Operand2 = value End Set End Property Public Function GetSum() As Integer GetSum = Operand1 + Operand2 End Function Public Overrides Function ToString() As String ToString = Operand1 & " + " & Operand2 & " = " & GetSum() End Function End Class Module Exercise3Task Sub Main() Dim airport1 As Airport = New Airport("ZAV", "Brussels") Dim airport2 As Airport = New Airport("NYK", "New York Kennedy") ' Dim flight1 As Flight = New Flight(airport1, airport2) Dim flight2 As Flight = New Flight(airport2, airport1) ' Dim holiday1 As Holiday = New Holiday holiday1.Add(flight1) holiday1.Add(flight2) ' For index As Integer = 0 To holiday1.Count - 1 Console.WriteLine(holiday1.Item(index).ToString()) Next ' Console.ReadLine() End Sub End Module Class Holiday Private m_Items As Flight() Private m_Count As Integer Public ReadOnly Property Count() As Integer Get Count = m_Count End Get End Property Public Sub Add(ByVal flight As Flight) ReDim Preserve m_Items(Count) m_Items(Count) = flight m_Count += 1 End Sub Default Public ReadOnly Property Item(ByVal index As Integer) As Flight Get Item = m_Items(index) End Get End Property End Class Class Flight Public Sub New(ByVal departure As Airport, ByVal arrival As Airport) Me.Departure = departure Me.Arrival = arrival End Sub Private m_Departure As Airport Public Property Departure() As Airport Get Departure = m_Departure End Get Set(ByVal value As Airport) m_Departure = value End Set End Property Private m_Arrival As Airport Public Property Arrival() As Airport Get Arrival = m_Arrival End Get Set(ByVal value As Airport) m_Arrival = value End Set End Property Public Overrides Function ToString() As String ToString = Departure.ToString() & " -> " & Arrival.ToString() End Function End Class Class Airport Public Sub New(ByVal code As String, ByVal Name As String) Me.Code = code Me.Name = Name End Sub Private m_Code As String Public Property Code() As String Get Code = m_Code End Get Set(ByVal value As String) m_Code = value End Set End Property Private m_Name As String Public Property Name() As String Get Name = m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property Public Overrides Function ToString() As String ToString = Code & " : " & Name End Function End Class 'Bezoek www.vbvoorbeelden.be voor meer Visual Basic voorbeelden. 'Copyright - De Wolf / vbvoorbeelden - 2003-2011 - Alle rechten voorbehouden.