OOP and Inheritance: "Is-a" Relationships : Inheritance « Class « Python Tutorial






class Employee:
    def __init__(self, name, salary=0):
        self.name   = name
        self.salary = salary
    def giveRaise(self, percent):
        self.salary = self.salary + (self.salary * percent)
    def work(self):
        print self.name, "does stuff"
    def __repr__(self):
        return "<Employee: name=%s, salary=%s>" % (self.name, self.salary)

class Developer(Employee):
    def __init__(self, name):
        Employee.__init__(self, name, 50000)
    def work(self):
        print self.name, "makes food"

class Server(Employee):
    def __init__(self, name):
        Employee.__init__(self, name, 40000)
    def work(self):
        print self.name, "interfaces with customer"

class Tester(Developer):
    def __init__(self, name):
        Developer.__init__(self, name)
    def work(self):
        print self.name, "makes pizza"

bob = Tester('bob')       
print bob                     # Run inherited __repr__
bob.work(  )                  # Run type-specific action
bob.giveRaise(0.20)           # Give bob a 20% raise
print bob; print

for klass in Employee, Developer, Server, Tester:
    obj = klass(klass.__name__)
    obj.work(  )








11.11.Inheritance
11.11.1.Inherit from two base classes
11.11.2.OOP and Inheritance: "Is-a" Relationships
11.11.3.Specializing Inherited Methods
11.11.4.Classes Are Customized by Inheritance