' VB6 code.
Public Function AddInts(ByVal x As Integer, ByVal y As Integer) As Integer
AddInts = x + y
End Function
Public Function AddDoubles(ByVal x As Double, ByVal y As Double) As Double
AddDoubles = x + y
End Function
Public Function AddLongs(ByVal x As Long, ByVal y As Long) As Long
AddLongs = x + y
End Function
Not only can code such as this become tough to maintain, but the caller must now be painfully
aware of the name of each method. Using overloading, we are able to allow the caller to call a single
method named Add(). Again, the key is to ensure that each version of the method has a distinct set
of arguments (members differing only by return type are not unique enough). Consider the follow-
ing class definition:
// C# code.
class Program
{
static void Main(string[] args) { }
Do'stlaringiz bilan baham: |