Tuesday, October 25, 2011

Overloading

Can not believe it for an overloading in programming OOP (Object Oriented Programming), It was very in need in a class called repeatedly with different parameters in the same class and the same method, it does not mean overriding. As in VB.Net:
'with difference parameters on Time
Module overLoadingVB
Sub Main()
' use overloaded constructors
Dim time1 As New timeOn()
Dim time2 As New timeOn(4)
Dim time3 As New timeOn(24, 34)
Dim time4 As New timeOn(time3) ' use time4 as initial value
Console.WriteLine(time1.stringUniversal() )
Console.WriteLine(time2.stringUniversal() )
Console.WriteLine(time3.stringUniversal())
Console.WriteLine(time4.stringUniversal())
End Sub ' Main
End Module
Class timeOn
Inherits Object
Private mHour As Integer ' 0 - 23
Private mMinute As Integer ' 0 - 59
Private mSecond As Integer ' 0 - 59
Public Sub New() ' Overloading Constructors default
setTime()
End Sub ' New
Public Sub New(ByVal hourValue As Integer) ' Overloading Constructors 1 Parameter
setTime(hourValue)
End Sub ' New
Public Sub New(ByVal hourValue As Integer, _ ' Overloading Constructors 2 Parameter
ByVal minuteValue As Integer)
setTime(hourValue, minuteValue)
End Sub ' New
Public Sub New(ByVal timeValue As timeOn) ' Overloading Constructors with class on Constructors 2 Parameters
setTime(timeValue.mHour, timeValue.mMinute)
End Sub ' New
Public Sub setTime(Optional ByVal hourValue As Integer = 0, _
Optional ByVal minuteValue As Integer = 0, _
Optional ByVal secondValue As Integer = 0)
If (hourValue >= 0 AndAlso hourValue < 24) Then
mHour = hourValue
Else
mHour = 0
End If
If (minuteValue >= 0 AndAlso minuteValue < 60) Then
mMinute = minuteValue
Else
mMinute = 0
End If
If (secondValue >= 0 AndAlso secondValue < 60) Then
mSecond = secondValue
Else
mSecond = 0
End If
End Sub ' setTime
Public Function stringUniversal() As String 'String Universal
Return String.Format("{0}:{1:D2}:{2:D2}", _
mHour, mMinute, mSecond)
End Function ' stringUniversal
End Class
view raw gistfile1.txt hosted with ❤ by GitHub


different from the python, in python there are several constructors, but it's rather difficult when compared to Java or Dot Net. like the __init__ constructor, the constructor can not be overloading the constructor of the same name before, just be overloading the operator or by the same constructor, but do not know in their use?
misalnya seperti :

class com:
    def __init__(self):
        print "original constructor"
    def __init__(self, val1):
        print "second overloading"
    def __init__(self, val1, val2):
        print "thrid overload"
this is wrong. because in python does not apply to any such provisions.
What about python?


No comments:

Post a Comment