[Tutor] Python's OOP-Inheritance make me confuse.
Moore John
moorejohn90 at googlemail.com
Sat Jan 19 08:08:27 CET 2013
Hi, I am new to Python language.
I have only 10 days experience on it.
When I start learning there is no difficult, but it make me slow down when
I reach "Object Oriented Concept", especially "Inherited".
Some of my background knowledge about "Inherited is the child class can get
all of characteristic and behaviour of parent class, in other words - data
and methods of parent".
Ok, I am going to show the concept that make me confuse with two programs.
Both of them are getting the same result, so why people are making
different.
-----------------------------------Frist----------------------------------------------------------
class Parent():
parentdata = 0
def __init__(self):
pass
def getParentData(self):
return Parent.parentdata
def setParentData(self, setdata):
Parent.parentdata = setdata
class Child(Parent):
childdata = 0
def __init__(self):
pass
def getChildData(self):
return Child.childdata
def setChildData(self, setdata):
Child.childdata = setdata
child = Child()
print "Default Child's Data is :" + str(child.getChildData())#getting 0
child.setChildData(3)
print "After Adding Child's Data is :"+ str(child.getChildData()) # getting
3
print "Default Parent's Data is:"+ str(child.getParentData())# getting 0
child.setParentData(1)
print "After Adding Parent's Data is :"+str(child.getParentData())# getting
1
-----------------------------Second-------------------------------------------------------------
class Parent():
parentdata = 0
def __init__(self):
pass
def getParentData(self):
return Parent.parentdata
def setParentData(self, setdata):
Parent.parentdata = setdata
class Child(Parent):
childdata = 0
def __init__(self):
#super(Child, self).__init__()
#super(Child, self).__init__(self, self)
Parent.__init__(self)
def getChildData(self):
return Child.childdata
def setChildData(self, setdata):
Child.childdata = setdata
child = Child()
print "Default Child's Data is :" + str(child.getChildData())#getting 0
child.setChildData(3)
print "After Adding Child's Data is :"+ str(child.getChildData()) # getting
3
print "Default Parent's Data is:"+ str(child.getParentData())# getting 0
child.setParentData(1)
print "After Adding Parent's Data is :"+str(child.getParentData())# getting
1
-----------------------------------------------------------------------------------------------
And also guide me, how to use "Super()" method for instance of
"Parent.__init__(self)
Somebody used, Super method ih there and some are doing as my way.
I am not clearly these two different.
In these two program - I am not using "__init__" as constructor.
If I am going to use "__init__" as to add data into the class's
data(childdata, parentdata), how do I insert parameter in
"Parent.__init__(self)" and both of their
"def __init__(self):" method.
Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130119/5ee32807/attachment-0001.html>
More information about the Tutor
mailing list