Section 13.2 A change of perspective
Throughout the earlier chapters, we wrote functions and called them using a syntax such as
printRange(lst)
. This suggests that the function is the active agent. It says something like, "Hey, printRange! Here’s a list for you to use to print with."In object-oriented programming, the objects are considered the active agents. This change in perspective is sometimes considered to be a more "polite" way to write programming instructions. However, it may not initially be obvious that it is useful. It turns out that often times shifting responsibility from the functions onto the objects makes it possible to write more versatile functions and makes it easier to maintain and reuse code.
The most important advantage of the object-oriented style is that it fits our mental chunking and real-life experience more accurately. In real life our
cook
method is part of our microwave oven — we don’t have a cook
function sitting in the corner of the kitchen, into which we pass the microwave! Similarly, we use the cellphone’s own methods to send an sms, or to change its state to silent. The functionality of real-world objects tends to be tightly bound up inside the objects themselves. OOP allows us to accurately mirror this when we organize our programs.You have attempted of activities on this page.