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

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.

主站蜘蛛池模板: 韶山市| 黔南| 曲阳县| 新邵县| 辽宁省| 哈密市| 浦县| 阳高县| 稻城县| 灯塔市| 黄龙县| 怀远县| 利川市| 大理市| 赣榆县| 平湖市| 滕州市| 长寿区| 高唐县| 临颍县| 巨野县| 泰兴市| 襄汾县| 固始县| 苏尼特左旗| 溧阳市| 贵阳市| 枝江市| 门头沟区| 大姚县| 罗源县| 万盛区| 苏尼特左旗| 泸西县| 垣曲县| 房山区| 焦作市| 凤翔县| 汽车| 西乡县| 会泽县|