- Hands-On Penetration Testing with Python
- Furqan Khan
- 133字
- 2021-07-02 14:14:00
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.
推薦閱讀
- GAE編程指南
- Implementing Modern DevOps
- 微服務與事件驅動架構
- Building a RESTful Web Service with Spring
- JavaScript前端開發與實例教程(微課視頻版)
- Java設計模式及實踐
- concrete5 Cookbook
- Unreal Engine 4 Shaders and Effects Cookbook
- QGIS By Example
- Mastering C++ Multithreading
- 輕松上手2D游戲開發:Unity入門
- Java SE實踐教程
- 快速入門與進階:Creo 4·0全實例精講
- Rust游戲開發實戰
- JavaScript程序設計(第2版)