官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 岳普湖县| 萝北县| 东辽县| 彰武县| 化州市| 确山县| 巴彦淖尔市| 金阳县| 湘潭市| 古蔺县| 阳江市| 天长市| 佳木斯市| 黔西县| 措美县| 石首市| 兴和县| 菏泽市| 河津市| 太康县| 东源县| 赫章县| 襄城县| 周至县| 耒阳市| 昆山市| 赤壁市| 喜德县| 益阳市| 买车| 伽师县| 营山县| 错那县| 利津县| 称多县| 措勤县| 乐至县| 宣威市| 赤峰市| 兴仁县| 灯塔市|