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

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.

主站蜘蛛池模板: 盈江县| 关岭| 射洪县| 乌拉特中旗| 奉贤区| 公主岭市| 喀喇| 道孚县| 瑞昌市| 洛浦县| 瓦房店市| 安化县| 平阴县| 右玉县| 东安县| 吉木乃县| 平塘县| 公主岭市| 鲁山县| 大英县| 龙陵县| 黄平县| 潜山县| 闽清县| 呼和浩特市| 林甸县| 三河市| 太谷县| 沙田区| 敦煌市| 壶关县| 盐津县| 千阳县| 延安市| 洛阳市| 高青县| 五常市| 盐边县| 罗源县| 顺平县| 沈丘县|