Inheriting from Aircraft

Now for the derived classes. We specify inheritance in Python using parentheses containing the base class name immediately after the class name in the class statement.

Here's the Airbus class:

class AirbusA319(Aircraft):

def __init__(self, registration):
self._registration = registration

def registration(self):
return self._registration

def model(self):
return "Airbus A319"

def seating_plan(self):
return range(1, 23), "ABCDEF"

And this is the Boeing class:

class Boeing777(Aircraft):

def __init__(self, registration):
self._registration = registration

def registration(self):
return self._registration

def model(self):
return "Boeing 777"

def seating_plan(self):
# For simplicity's sake, we ignore complex
# seating arrangement for first-class
return range(1, 56), "ABCDEGHJK"

Let's exercise them at the REPL:

>>> from airtravel import *
>>> a = AirbusA319("G-EZBT")
>>> a.num_seats()
132
>>> b = Boeing777("N717AN")
>>> b.num_seats()
495

We can see that both subtype aircraft inherited the num_seats() method, which now works as expected because the call to seating_plan() is successfully resolved on the self object at runtime.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset