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

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.

主站蜘蛛池模板: 大新县| 崇信县| 柳林县| 滨海县| 海丰县| 于都县| 晋中市| 于都县| 水城县| 丁青县| 调兵山市| 井研县| 泾川县| 清涧县| 文昌市| 丽水市| 达拉特旗| 常宁市| 义马市| 山东省| 通河县| 铜陵市| 靖西县| 甘德县| 隆化县| 三台县| 隆安县| 梅州市| 苗栗市| 闸北区| 许昌县| 柳江县| 宁国市| 高青县| 建瓯市| 盘锦市| 临洮县| 玉田县| 徐水县| 兴业县| 张北县|