Polymorphism with functions

Let's think about two classes, Ferrari and McLaren. Let's assume that both have a Speed() method that returns the top speed of the cars. Let's think about how can we use function polymorphism in this scenario. Let's create a file called Poly_functions.py:

We can see that we have two classes, Ferrari and McLaren. Both have a common speed method that prints the speed of the two cars. One approach would be to create instances of both the classes and invoke the print speed method with each instance. Another approach could be to create a common method that takes the instance of the classes and that invokes the speed method on the instances it receives. This is the polymorphic printSpeed(carType) function that we defined at line 10.

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

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