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

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.

主站蜘蛛池模板: 驻马店市| 体育| 兰西县| 高安市| 香河县| 壤塘县| 慈利县| 宿松县| 运城市| 锦屏县| 靖宇县| 贡山| 临泉县| 湄潭县| 保亭| 宿州市| 那坡县| 运城市| 高邑县| 五台县| 沁源县| 柳州市| 古浪县| 乌兰浩特市| 怀宁县| 上饶市| 齐河县| 汤原县| 二连浩特市| 重庆市| 罗田县| 安化县| 尚义县| 吴忠市| 东莞市| 鄂托克前旗| 汤原县| 南华县| 淮阳县| 曲水县| 自治县|