Main.Inheritance History

Hide minor edits - Show changes to markup

April 08, 2010, at 09:40 PM MST by 71.196.160.210 -
Added lines 1-112:

(:source lang=python:)

"""Inheritance in python """

  1. This module defines several types of shapes (Rectangle, Circle)
  2. using inheritance from a base class called Shape.
  3. In inheritance:
  4. The more general class is called the base class or superclass.
  5. The more specific class is called the derived class or subclass.
  6. First we will need the Point class we have already defined:

import math class Point (object) :

    """Point is a class that stores a two dimensional point"""
    def __init__(self, x=0, y=0) :
        self.x = x
        self.y = y
    def distance_to_origin(self) :
        """compute the distance of a point from the origin"""
        return math.sqrt(self.x*self.x + self.y *self.y)
    def distance(self, other) :
        """compute the distance between a point object and another
        point object"""
        return math.sqrt((self.x - other.x)**2 + 
                         (self.y - other.y)**2)
    def duplicate(self) :
        return Point(self.x, self.y)
    def __repr__(self) :
        return "Point object with x = d" % (self.x, self.y)

class Shape (object) :

    def __init__(self, x=0, y=0) :
        """creates an instance of a Shape with a location given
        by x and y"""
        self.location = Point(x, y)
    def move(self, x, y) :
        """move a shape object to a location given by x and y"""
        self.location = Point(x, y)
    def distance_to_origin(self) :
        return self.location.distance_to_origin()
    def distance(other) :
        return self.location.distance(other.location)
    def __repr__(self) :
        return "A shape located at: " + str(self.location)

class Rectangle (Shape) :

    """A class that represents a rectangle"""
    def __init__(self, width=1, height=1, x=0, y=0) :
        """width - the width of the rectangle
        height - the height of the rectangle"""
        self.width = width
        self.height = height
        Shape.__init__(self, x, y)
    def duplicate(self) :
        return Rectangle(self.width, self.height, self.x, self.y)
    def area(self) :
        """returns the area of the rectangle"""
        return self.width * self.height
    def perimeter(self) :
        """returns the perimiter of the rectangle"""
        return 2 * self.width + 2 * self.height
    def scale(self, s) :
        """rescale the size of the rectangle by the given amount"""
        self.height *= s
        self.width *= s
    def __repr__(self) :
        return "Rectangle with width = f" % (self.width, self.height)

rect1 = Rectangle(1, 2) rect2 = Rectangle()

  1. When we define a method in the derived class it overrides the corresponding
  2. method from the base class.
  3. Note that you have to call the constructor of the base class explicitly:
  4. Shape.__init__(self, location)

import math class Circle (Shape) :

    """A class that represents a circle"""
    def __init__(self, radius=1, x=0, y=0) :
        """radius - the radius of the circle"""
        Shape.__init__(self, x, y)
        self.radius = radius
    def duplicate(self) :
        return Circle(self.radius, self.x, self.y)
    def area(self) :
        """returns the area of the rectangle"""
        return math.pi * self.radius * self.radius
    def perimeter(self) :
        """returns the perimiter of the rectangle"""
        return 2 * math.pi * self.radius
    def scale(self, s) :
        """rescale the size of the rectangle by the given amount"""
        self.radius *= s
    def __repr__(self) :
        return "Circle with radius =  self.radius

circ1 = Circle(2) circ2 = Circle()

  1. Why use inheritance?
  2. - Code reuse - code from the base class does not need to be repeated.
  3. Leads to easier maintenance of the code.
  4. - Adding functionality to the base class automatically reflects in the
  5. Derived classes.

(:sourceend:)