Methods Overloading
Class Calculator
Overloads Public Function Add(a As Integer, b As Integer) As Integer
Add = a + b
End Function
Overloads Public Function Add(a As Double, b As Double) As Double
Add = a + b
End Function
End Class
Overloads Public Function Add(a As Integer, b As Integer) As Integer
Add = a + b
End Function
Overloads Public Function Add(a As Double, b As Double) As Double
Add = a + b
End Function
End Class
as you can see we have two methods with the same name 'Add', but with two different signatures, it accepts the first parameter 'a' and 'b' as a integer, the second as duoble.
to use two methods:
to use two methods:
Dim counter As Calculator
counter = New Calculator()
' pass two integers
Console.WriteLine(counter.Add(1, 5))
' pass two doubles
Console.WriteLine(counter.Add(1.3, 5.9))
counter = New Calculator()
' pass two integers
Console.WriteLine(counter.Add(1, 5))
' pass two doubles
Console.WriteLine(counter.Add(1.3, 5.9))
0 commenti:
Posta un commento