Chapter 10: Interfaces: Multiple Inheritance

example 10.1, page no. page no. 184

In [1]:
"""
There are no interfaces in Python. We will use normal classes instead
"""

class Rectangle:
    def compute(self, x, y):
        return x*y

class Circle:
    pi = 3.14
    def compute(self, x, y):
        return self.pi*x*x

rect = Rectangle()
cir = Circle()
print "Area of rectangle: ", rect.compute(10, 20)
print "Area of circle: ", cir.compute(10, 0)
Area of rectangle:  200
Area of circle:  314.0

example 10.2, page no. 211

In [9]:
class Student(object):
    rollNumber = 0
    def getNumber(self, n):
        self.rollNumber = n
    def putNumber(self):
        print "Roll No.: ", self.rollNumber

class Test(Student, object):
    part1 = 0.0
    part2 = 0.0
    def getMarks(self, m1, m2):
        self.part1 = m1
        self.part2 = m2
    def putMarks(self):
        print "Marks Obtained"
        print "Part 1  = ", self.part1
        print "Part 2 = ", self.part2

class Results(Test):
    sportWt = 6.0
    def putWt(self):
        print "Sports Wt: ", self.sportWt
    def display(self):
        total = self.part1 + self.part2 + self.sportWt
        self.putNumber()
        self.putMarks()
        self.putWt()
        print "Total Score: ", total

student1 = Results()
student1.getNumber(1234)
student1.getMarks(27.5, 33.0)
student1.display()
Roll No.:  1234
Marks Obtained
Part 1  =  27.5
Part 2 =  33.0
Sports Wt:  6.0
Total Score:  66.5