| |||||||||||
| |||||||||||
Inheritance
In (Object Oriented) programming terms, "Inheritance" is used to define a type of data (an "object") which behaves like another type of data with some additions and differences. For example, we can define a type of data that's a piece of clothing. We can then define a pair of shoes which is like a piece of clothing, except that it has two extra pieces of information involved - a length and a width. Languages that are known as Object Oriented languages provide facilities to make such definitions easy without repeating code. In the following piece of code (in Python), we define a piece of clothing (it's known as a base class) and then we extend it into a new class (called a subclass) which has the same code and properties as a piece of clothing, with the exception of changes that we list. The tshirt and tights classes are also subclasses of clothing and we can make up a list of pieces of clothing (you'll see it done in the test code at the bottom) which we can then loop through reporting colour and size. As we loop through in this way, the getcolour method is INHERITED from the clothes base class in each case (i.e. it is common code) and the getsize method uses what we call POLYMORPHISM as it uses different code to return the size for different types of clothing. class clothing: def __init__(self,colour,weight): self.colour=colour self.weight=weight def getcolour(self): return self.colour def getsize(self): return "n/a" class shoes(clothing): def __init__(self,colour,weight,len,wid): clothing.__init__(self,colour,weight) self.length = len self.width = wid def getsize(self): r = "Length "+str(self.length)+" and width "+self.width return r class tshirt(clothing): def __init__(self,colour,weight,size): clothing.__init__(self,colour,weight) self.size = size def getsize(self): return self.size class tights(clothing): pass if __name__ == "__main__": wearing = [] wearing.append(tshirt("blue",1.0,"XL")) wearing.append(tights("pink",0.1)) wearing.append(shoes("brown",2.0,11,"D")) for haveon in wearing: print "I am wearing something",haveon.getcolour(), print " size ",haveon.getsize() Let's run that ... [localhost:~/ipy] graham% python dresser I am wearing something blue size XL I am wearing something pink size n/a I am wearing something brown size Length 11 and width D [localhost:~/ipy] graham% See also further examples and Python object training Please note that articles in this section of our
web site were current and correct to the best of our ability when published,
but by the nature of our business may go out of date quite quickly. The
quoting of a price, contract term or any other information in this area of
our website is NOT an offer to supply now on those terms - please check
back via our main web site
Related Material
Python - Objects - Intermediate resource index - Python Solutions centre home page You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum. At Well House Consultants, we provide training courses on subjects such as Ruby, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site. |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||