Rev. | fcdbae37c02af2fee493957a2dedf401587ab194 |
---|---|
大小 | 1,099 字节 |
时间 | 2007-08-05 08:50:36 |
作者 | iselllo |
Log Message | I added a derivated class (using inheritance) to the code object2.py. |
#! /usr/bin/env python
"""square.py -- Make some noise about a square."""
#first I create a class rectangle
class rectangle:
#I initialize the dimensions of the rectangle
def __init__(self, height, width):
self.height = height
self.width = width
#then I calculate its area
def area(self):
return self.height * self.width
#Now I define a derived class from the previous one using inheritance
#this simply returns on top of the previous stuff twice the are of the rectangle
class rect_new(rectangle): # I have to provide the name of the previous class as an
# argument, since that is the class I want to derive from
def double_area(self):
return rectangle.area(self)*2. #now the new class can do anything the
#old one was able to do plus calculate the new area
my_rect=rectangle(5.,4.)
print 'the width is ', my_rect.width
print 'the height', my_rect.height
print 'the area is', my_rect.area()
# now a test on inheritance
my_rect2=rect_new(5.,4.)
print 'double area is', my_rect2.double_area()
print 'the original area calculate by the inherited class is ', my_rect2.area()