22.3 OOP and Inheritance: "is-a" Relationships
We've talked about the mechanics of inheritance in depth already, but we'd like to show you an example of how it can be used to model real-world relationships. From a programmer's point of view, inheritance is kicked off by attribute qualifications, and triggers a search for a
To
Our pizza shop team can be defined by the following classes in the example file
employees.py
. It defines four classes and some self-test code. The most general class,
Employee
, provides common behavior such as
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 Chef(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 PizzaRobot(Chef):
def __init__(self, name):
Chef.__init__(self, name)
def work(self):
print self.name, "makes pizza"
if __name__ == "__main__":
bob = PizzaRobot('bob') # Make a robot named bob.
print bob # Runs inherited __repr__
bob.work( ) # Run type-specific action.
bob.giveRaise(0.20) # Give bob a 20% raise.
print bob; print
for klass in Employee, Chef, Server, PizzaRobot:
obj = klass(klass.__name__)
obj.work( )
When we run this module's self-test code, we create a pizza-making robot named
bob
, which inherits
C:\python\examples> python employees.py <Employee: name=bob, salary=50000> bob makes pizza <Employee: name=bob, salary=60000.0> Employee does stuff Chef makes food Server interfaces with customer PizzaRobot makes pizza In a class hierarchy like this, you can usually make instances of any of the classes, not just the ones at the bottom. For instance, the for loop in this module's self-test code creates instances of all four classes; each responds differently when asked to work, because the work method is different in each. Really, these classes just simulate real world objects; work prints a message for the time being, but could be expanded to really work later. |