June 2017
Beginner
352 pages
8h 39m
English
As usual, this will make much more sense with an example. We would like our aircraft classes AirbusA319 and Boeing777 to provide a way of returning the total number of seats. We’ll add a method called num_seats() to both classes to do this:
def num_seats(self): rows, row_seats = self.seating_plan() return len(rows) * len(row_seats)
The implementation can be identical in both classes, since it can be calculated from the seating plan.
Unfortunately, we now have duplicate code across two classes, and as we add more aircraft types the code duplication will worsen.
The solution is to extract the common elements of AirbusA319 and Boeing777 into a base class from which both aircraft types will derive. Let's recreate the ...
Read now
Unlock full access